本文介绍了传递改变的价值异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的code,我增加了价值梳理,试图使用该值在异步函数,然后改变梳的价值。在请求梳子值不作为,因为comb.pop所需的()之前申请的回调火灾发生多次。搜索在堆栈溢出许多问题之后,我试图把各地的要求,即把梳子作为输入关闭,但没有奏效。我应该如何去吗?

 梳= [1,2,3];
ARR = [10,20,30]。
承诺= [];
对于(VAR I = 0; I< arr.length;我++){
    comb.push(ARR [I]);    VAR诺=新的承诺(函数(解析,拒绝){
        请求(URL,功能(呃,RES体){
            //使用梳子
            如果(/ *梳子满足一定的条件* /)
                解决(体);
        });
    });
    promises.push(承诺);    comb.pop();
}

下面是我在尝试使用闭包,没有工作:

  VAR承诺=新的承诺(函数(解析,拒绝){
    (函数(梳){
        请求(URL,功能(呃,RES体){
            //使用梳子
            如果(/ *梳子满足一定的条件* /)
                解决(体);
        });
    })(梳);
});


解决方案

这将简单的工种:

  VAR承诺=(函数(combCopy){
     VAR RET =新的承诺(函数(解析,拒绝){
          请求(URL,功能(呃,RES体){
              //梳指外梳
              // combCopy指副本
          });
      });
     返回RET;
   })(梳);

对象总是被这样传递一个对象作为参数不会复制对象引用传递。你需要的做手工。

  VAR承诺=(函数(){
     //这将创建一个新的范围 - 这可能没有必要
     VAR combCopy = Array.prototype.slice.call(梳); //浅只
     VAR RET =新的承诺(函数(解析,拒绝){
          请求(URL,功能(呃,RES体){
              //梳指外梳
              // combCopy指副本
          });
      });
      返回RET;
   })();

In the code below, I am adding a value to comb, trying to use that value in an asynchronous function, and then changing the value of comb. The value of comb within request is not as desired since comb.pop() occurs multiple times before request's callback fires. After searching many questions on stack overflow, I have tried putting a closure around request that took comb as input, but that did not work. How should I go about this?

comb = [1,2,3];
arr = [10,20,30];
promises = [];
for (var i = 0; i < arr.length; i++) {
    comb.push(arr[i]);

    var promise = new Promise(function(resolve, reject) {
        request(url, function(err, res, body) {
            // use comb
            if (/* comb meets certain condition */)
                resolve(body);
        });
    });
    promises.push(promise);

    comb.pop();
}

Here is my attempt at using a closure, which did not work:

var promise = new Promise(function(resolve, reject) {
    (function(comb) {
        request(url, function(err, res, body) {
            // use comb
            if (/* comb meets certain condition */)
                resolve(body);
        });
    })(comb);
});
解决方案

This will work for simple types:

  var promise = (function(combCopy){
     var ret = new Promise(function(resolve, reject) {
          request(url, function(err, res, body) {
              //comb refers to the outside comb
              //combCopy refers to the copy
          });
      });
     return ret;
   })(comb);

Objects are always passed by references so passing an object as a parameter won't copy the object. You'll need to do that manually.

   var promise = (function(){ 
     //this creates a new scope -- something that might not be necessary
     var combCopy = Array.prototype.slice.call(comb); //shallow-only
     var ret = new Promise(function(resolve, reject) {
          request(url, function(err, res, body) {
              //comb refers to the outside comb
              //combCopy refers to the copy
          });
      });
      return ret;
   })();

这篇关于传递改变的价值异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 04:03