首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何管理对异步服务的调用的可变计数

如何管理对异步服务的调用的可变计数
EN

Stack Overflow用户
提问于 2018-08-05 07:07:56
回答 3查看 146关注 0票数 3

给定的

代码语言:javascript
运行
复制
[1,2,3,4,5] variable array of numbers

我想为数组中的每个数字调用一个异步方法。有什么方法可以解决这个问题呢?异步方法的返回类型为JSON。

我期望的是一个新的JSON数组,其中包含调用的结果

代码语言:javascript
运行
复制
[{id:1,name:"name1"},
{id:2,name:"name2"},
{id:3,name:"name3"},
{id:4,name:"name4"},
{id:5,name:"name5"}]

最新结果

app.js

代码语言:javascript
运行
复制
( () => {
    const result =  adapterContext.get([1, 2, 3]);
    console.log(result);
})();

service.js

代码语言:javascript
运行
复制
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(结果)未定义?

代码语言:javascript
运行
复制
undefined  
response [ '{"hello":1}', '{"hello":2}', '{"hello":3}' ]  
EN

Stack Overflow用户

发布于 2018-08-05 08:10:35

好的,这里有一些东西可以帮助你,运行代码片段,看看结果是什么样子。

代码语言:javascript
运行
复制
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);
});

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51690200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档