2017-08-25 81 views
1

问题异步函数,该函数不等到地图将完成如何使基于承诺

我认为解决方案是Promise.all 但我不知道如何使用此功能

exports.createProduct = (data) => { 
    var imgS3arr = []; //array which we waiting for 
    data.img.map((img, i) => { 
    fetch.remote(img).then((base) => { 
     var buf = Buffer.from(base[0], 'base64'); 

     var imgS3 = { 
      Key: data.title + i, //TODO: add random 
      Body: buf, 
      ContentEncoding: 'base64', 
      ContentType: 'image/jpeg' 
     }; 

     s3Bucket.putObject(imgS3, function (err, data) { 
      if (err) { 
       console.log(err); 
       console.log('Error uploading data: ', data); 
      } else { 
       var params = this.request.params; 
       var region = this.request.httpRequest.region; 
       imgS3arr.push('https://s3-' + region + '.amazonaws.com/' + params.Bucket + '/' + params.Key) 
      } 
     } 
    ); 
    }).catch((reason) => { }); 
}); 

//next part of code must be executed when all items pushed to 'imgS3arr' 

const product = new Product({ 
    title: data.title, 
    img: imgS3arr, //we waiting for this array 
}); 
return product.save((function (err) { 
    if (err) { 
     console.log(err); 
    } else { 
     console.log('Added new! \n' + product); 
    } 
    })); 
} 

可有一个人帮我这个麻烦?

+0

您混合定期回调和承诺异步。首先要做的就是将这段代码中的所有异步操作转换为promise。然后,用'Promise.all()'等待将是微不足道的。 – jfriend00

回答

0

你可以做这样的事情:

// returns a promise that is resolved when everything is done 
// or rejected with the first error that occurs 
exports.createProduct = (data) => { 
    var imgS3arr = []; //array which we waiting for 
    return Promise.all(data.img.map((img, i) => { 
     return fetch.remote(img).then((base) => { 
      var buf = Buffer.from(base[0], 'base64'); 

      var imgS3 = { 
       Key: data.title + i, //TODO: add random 
       Body: buf, 
       ContentEncoding: 'base64', 
       ContentType: 'image/jpeg' 
      }; 

      return s3Bucket.putObject(imgS3).promise().then(function(data) { 
       // given other changes here and unfamiliarity with this code 
       // I'm unsure if `this` still has the right value in it here 
       var params = this.request.params; 
       var region = this.request.httpRequest.region; 
       imgS3arr.push('https://s3-' + region + '.amazonaws.com/' + params.Bucket + '/' + params.Key) 
      }).catch(function(err) { 
       // log error, then rethrow to keep the promise rejected 
       console.log(err); 
       console.log('Error uploading data: ', data); 
       throw err; 
      }); 
     }); 
    })).then(function() { 
      const product = new Product({ 
       title: data.title, 
       img: imgS3arr, //we waiting for this array 
      }); 
      // use database interface that uses promises, not plain callbacks 
      return product.save(...); 
     } 
    }); 
} 

总结和解释所做的更改:

  1. 里面的data.img.map(),返回一个承诺,因此.map()创建承诺的数组。
  2. 承诺进行阵列上使用Promise.all()知道,当他们完成所有的工作。
  3. 切换到s3Bucket.putObject(imgS3).promise()所以我们可以使用的承诺,而不是简单的回调。
  4. 重新抛出错误.catch()登录状态,这样的承诺后撑拒绝要等待Promise.all().then()处理器中
  5. 将代码。
  6. 切换product.save()的数据库接口使用的承诺,而不是简单的回调,所以你可以直接链中的承诺(左为OP做到,因为我们甚至不知道它是什么数据库)。