给定的:
[1,2,3,4,5] variable array of numbers我想为数组中的每个数字调用一个异步方法。有什么方法可以解决这个问题呢?异步方法的返回类型为JSON。
我期望的是一个新的JSON数组,其中包含调用的结果
[{id:1,name:"name1"},
{id:2,name:"name2"},
{id:3,name:"name3"},
{id:4,name:"name4"},
{id:5,name:"name5"}]最新结果
app.js
( () => {
    const result =  adapterContext.get([1, 2, 3]);
    console.log(result);
})();service.js
exports.get = list => {
    Promise.all(list.map(promise-method-call))
        .then(function (response) {
            console.log("response", response);
            return response;
        }).catch(function (error) {
            console.log("oops ", error);
        });
};结果
为什么console.log(结果)未定义?
undefined  
response [ '{"hello":1}', '{"hello":2}', '{"hello":3}' ]  发布于 2018-08-05 08:10:35
好的,这里有一些东西可以帮助你,运行代码片段,看看结果是什么样子。
var getAsyncObjectWithList = function(idList) {
  const promiseHandler = function(resolve, reject) {
      // fake async call, like http call
      setTimeout(function() {
        // fake conditional statement
        // for error cases
        if (!idList) { 
          reject("no ids specified");
          return;
        }
        // do actual processing here
        var newList = idList.map(function(id) {
          return { id: id, name: "name: "+id }
        });
        // return result by resolving list
        resolve(newList);
      },  3000)
  }
  return new Promise(promiseHandler);
}
// success case
getAsyncObjectWithList([1,2,3,4]).then(function(response) {
    console.log("response", response);
}).catch(function(error){
    console.log("oops ", error);
});
// error case
getAsyncObjectWithList().catch(function(error){
    console.log("oops ", error);
});
var getAsyncObjectWithId = function(id) {
  const promiseHandler = function(resolve, reject) {
      // fake async call, like http call
      setTimeout(function() {
          // fake conditional statement
          // for error cases
          if (!id) {
            reject("no id specified");
            return;
          }
          // do actual processing here
          var newObj = { id: id, name: "name: "+id };
          // return result by resolving object
          resolve(newObj);
      }, 3000);
  }
  return new Promise(promiseHandler);
}
// success case
Promise.all([1,2,4,5].map(getAsyncObjectWithId))
.then(function(response) {
    console.log("response", response);
}).catch(function(error){
    console.log("oops ", error);
});
// error case
getAsyncObjectWithId().catch(function(error){
    console.log("oops ", error);
});
https://stackoverflow.com/questions/51690200
复制相似问题