相关疑难解决方法(0)

使用ES6的Promise.all()时限制并发性的最佳方法是什么?

我有一些代码在一个列表中进行迭代,该列表从数据库中查询并为该列表中的每个元素发出HTTP请求.该列表有时可能是一个相当大的数字(成千上万),我想确保我没有遇到数千个并发HTTP请求的Web服务器.

此代码的缩写版本目前看起来像这样......

function getCounts() {
  return users.map(user => {
    return new Promise(resolve => {
      remoteServer.getCount(user) // makes an HTTP request
      .then(() => {
        /* snip */
        resolve();
      });
    });
  });
}

Promise.all(getCounts()).then(() => { /* snip */});
Run Code Online (Sandbox Code Playgroud)

此代码在Node 4.3.2上运行.重申Promise.all一下,可以进行管理,以便在任何给定时间只有一定数量的Promise正在进行中?

javascript node.js es6-promise

67
推荐指数
7
解决办法
3万
查看次数

在javascript中递归建立一个promise链 - 内存考虑因素

这个答案中,一个承诺链是递归建立的.

稍微简化,我们有:

function foo() {
    function doo() {
        // always return a promise
        if (/* more to do */) {
            return doSomethingAsync().then(doo);
        } else {
            return Promise.resolve();
        }
    }
    return doo(); // returns a promise
}
Run Code Online (Sandbox Code Playgroud)

据推测,这会产生一个调用堆栈一个承诺链 - 即"深"和"宽".

我预计内存峰值会大于执行递归或单独建立一个promise链.

  • 是这样吗?
  • 有没有人考虑过以这种方式建立连锁店的记忆问题?
  • 承诺库之间的内存消耗会有所不同吗?

javascript recursion promise

55
推荐指数
3
解决办法
1万
查看次数

JS 中 Promise.all() 和 Promise.allSettled() 的区别?

我正在阅读有关PromiseMDN手册,我发现这两种方法与我相似:

它们都接受一个可迭代对象并返回一个包含已完成的Promises的数组。

那么,它们之间有什么区别呢?

javascript es6-promise

31
推荐指数
4
解决办法
1万
查看次数

用Axios承诺所有人

我刚读了一篇与promise相关的文章,无法理解我们如何使用Axise通过Promise.all进行多个API调用.

所以考虑有3个URL,让我们这样称呼它

let URL1 = "https://www.something.com"
let URL2 = "https://www.something1.com
let URL3 = "https://www.something2.com"
Run Code Online (Sandbox Code Playgroud)

还有一个我们将存储Value的数组

  let promiseArray = []
Run Code Online (Sandbox Code Playgroud)

现在,我想并行运行(Promise.all),但我无法确定我们将如何做到这一点?因为axios本身就有一个承诺(或者至少我是如何使用它的).

axios.get(URL).then((response) => {
}).catch((error) => {
})
Run Code Online (Sandbox Code Playgroud)

问:有人可以告诉我我们如何使用promise.all和axios发送多个请求

javascript promise axios

20
推荐指数
6
解决办法
2万
查看次数

与Promise.all并行运行?

我被引导相信Promise.all会并行执行你传递它的所有函数,而不关心返回的promises完成的顺序.

但是当我写这个测试代码时:

function Promise1(){
    return new Promise(function(resolve, reject){
        for(let i = 0; i < 10; i++){
            console.log("Done Err!");
        }
        resolve(true)
    })
}

function Promise2(){
    return new Promise(function(resolve, reject){
        for(let i = 0; i < 10; i++){
            console.log("Done True!");
        }
        resolve(true)
    })
}

Promise.all([ 
    Promise1(),
    Promise2()
])
.then(function(){
    console.log("All Done!")
})
Run Code Online (Sandbox Code Playgroud)

我得到的结果就是这个

Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done True!
Done True!
Done True!
Done True!
Done …
Run Code Online (Sandbox Code Playgroud)

node.js promise bluebird

13
推荐指数
2
解决办法
1万
查看次数

一系列承诺如何与减少完全一致?

我在这里阅读这篇文章,它讲述了如何使用reduce with promises,最后显示以下代码片段:

const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
    return promiseChain.then(chainResults =>
        currentTask.then(currentResult =>
            [ ...chainResults, currentResult ]
        )
    );
}, Promise.resolve([])).then(arrayOfResults => {
    // Do something with all results
});
Run Code Online (Sandbox Code Playgroud)

所以在不改变太多代码的情况下,我做了以下演示:

const tasks = [ fetch('https://jsonplaceholder.typicode.com/todos/1') , 
                fetch('https://jsonplaceholder.typicode.com/todos/2') ,   
                fetch('https://jsonplaceholder.typicode.com/todos/3')  ];


tasks.reduce((promiseChain, currentTask) => {

    console.log(promiseChain);  

    return promiseChain.then(chainResults => {
        return currentTask.then(currentResult =>
            [ ...chainResults, currentResult ]
        )
    });
}, Promise.resolve([])).then(arrayOfResults => {
    // Do something with all results
    console.log(arrayOfResults);
});
Run Code Online (Sandbox Code Playgroud)

但我仍然不明白,因为reduce简单地只是一个forEach循环而内部减少我们依赖于一个返回的promise,reduce函数不会仅仅循环遍历数组中的所有元素(在此case一个promises数组),没有then()触发?

javascript es6-promise

6
推荐指数
1
解决办法
145
查看次数

我怎样才能限制角度$ q承诺并发?

我该怎么做$q.all但限制同时执行多少个promises?

我的问题就像我如何限制Q promise并发?

我希望一次产生的过程不超过5个

对于另一个问题,接受的答案是为了与Q一起工作而承诺的图书馆.但我特别感兴趣的是Angular的解决方案,而不是.$qQ

背景:问题正在解决:
我有两个步骤下载的文件:a)获取URL b)下载文件.
浏览器限制了可以同时检索多少文件,因此当直接使用promises时$q.all会触发所有下载,只有
N会立即发生,例如Chrome中的6,而其余​​文件会延迟.(请参阅浏览器中的Max并行http连接?)
问题是URL已过期,因此当浏览器执行第 N + 1 文件下载时,URL不再有效.
所以我想做的事情throttled.all(6, promises)而不是$q.all(promise)

concurrency promise angularjs angular-promise

5
推荐指数
1
解决办法
1382
查看次数

在瀑布链接承诺

我一直在玩几种不同的链接功能集合的方式,似乎找不到我特别喜欢的方法.以下是我最后决定但仍然不热衷的事情.

有人可以提出更清洁,更简洁的模式吗?我不想选择Async.js或库.

[
  this.connectDatabase.bind(this),
  this.connectServer.bind(this),
  this.listen.bind(this)
].reduce(
  (chain, fn) => {
    let p = new Promise(fn);
    chain.then(p);
    return p;
  },
  Promise.resolve()
);
Run Code Online (Sandbox Code Playgroud)

PS.任何其他提示都受到欢迎.

node.js es6-promise

5
推荐指数
1
解决办法
3102
查看次数