简体   繁体   English

Promise.all未能按预期工作

[英]Promise.all not working at expected

I expected the Promise.all code to return 我希望Promise.all代码能够返回

{ constant: 'x1' }
{ constant: 'x2' }
{ constant: 'x3' }

but instead I have this: 但是我有这个:

{ constant: 'x1' }
{ constant: 'x12' }
{ constant: 'x123' }

Why would this happen? 为什么会这样? I can not seem to see where 'constant' is accumulating. 我似乎看不到“常数”在哪里累积。

This is a simplified view - I actually updated/create a document in "testFN" 这是一个简化的视图-我实际上在“ testFN”中更新/创建了一个文档

thanks 谢谢

function testFN(blob, array, iter){
    var newBlob = blob;
    newBlob.constant = blob.constant+array.num
    console.log(newBlob);
    return products.findOneAndUpdate ....   
}

exports.updateProductId = function(req, res) {

    var blob = {"constant":"x"};
    var arr = { key1:{num: "1"}, key2:{num: "2"}, key3:{num: "3"}};
    var fnArr = [];

    Object.keys(arr).forEach(function(key, i) {
       fnArr.push(testFN(blob, arr[key], i));
    });

    return Promise.all(fnArr)
    .then(function(success){
        console.log(success);
    })
    .catch(function(error){
        console.log(error);
    })

})

Non-primitives are passed by reference - you can think of any variable that represents an object as a reference to a memory location . 非基元通过引用传递-您可以将代表对象的任何变量视为对内存位置的引用。 So, in testFN : 因此,在testFN

function testFN(blob, array, iter){
  var newBlob = blob;
  newBlob.constant = blob.constant+array.num

newBlob points to the same place in memory as the blob parameter, so when you change newBlob.constant , you change the original blob.constant as well (since they're the same object). newBlob指向内存中与blob参数相同的位置,因此,当您更改newBlob.constant ,您也将更改原始的blob.constant (因为它们是同一对象)。 You can fix it by assigning a true copy of blob to newBlob : 您可以通过将blob真实副本分配给newBlobnewBlob

 function testFN(blob, array, iter){ var newBlob = JSON.parse(JSON.stringify(blob)); newBlob.constant = blob.constant + array.num; console.log(newBlob); return newBlob; } var blob = {"constant":"x"}; var arr = { key1:{num: "1"}, key2:{num: "2"}, key3:{num: "3"}}; var fnArr = []; Object.keys(arr).forEach(function(key, i) { fnArr.push(testFN(blob, arr[key], i)); }); Promise.all(fnArr) .then(function(success){ console.log(success); }) .catch(function(error){ console.log(error); }) 

This has nothing to do with Promise.all . 这与Promise.all None of this code is asynchronous anyway. 无论如何,这些代码都不是异步的。

Promise.all can work with array of Promises change you function "testFN" to return Promise. Promise.all可以与Promises数组一起使用,更改函数“ testFN”以返回Promise。

function testFN(blob, array, iter){
    return new Promise(function(resolve, reject) {
       var newBlob = blob;
       newBlob.constant = blob.constant+array.num;
       console.log(newBlob);
       resolve();
    });
}

But you might what to see if you need Promise.all in this case at all. 但是在这种情况下,您可能会发现是否需要Promise.all。 Bcause you do not have any Async operation here 因为您在这里没有任何异步操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM