我是Node.js(Java背景)的新手,试图使用Promises一个接一个地运行一个Api调用。第一个Api调用(fetchIdAPI)将返回ID数组,然后对于每个ID,我需要运行第二个API调用。这就是我想出来的
const _ = require('lodash');
var Promise = require('bluebird');
let self = this;
const mockAPI = id => {
return self.callMarkRecipeAsUnCookedApi(user_id, id);
};
const fetchIdAPI = () => {
return self.callGetAllCookedApi(user_id);
};
const fetchIDList = () => new Promise((resolve, reject) => resolve(fetchIdAPI));
fetchIDList()
.then(idlist => Promise.all(_.map(idList, id => new Promise((resolve, reject) => resolve(mockAPI(id))))))
.then(data => {
console.log(data);
});
但是当我调用fetchIDList()promise时,我想我搞砸了,因为如果我将传递而不是fetchIdAPI()常规数组[1,2,3]。一切正常。那里的任何人都知道我该怎么做?