我编写的代码看起来像:
function getStuffDone(param) { | function getStuffDone(param) {
var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) {
// or = new $.Deferred() etc. | // using a promise constructor
myPromiseFn(param+1) | myPromiseFn(param+1)
.then(function(val) { /* or .done */ | .then(function(val) {
d.resolve(val); | resolve(val);
}).catch(function(err) { /* .fail */ | }).catch(function(err) {
d.reject(err); | reject(err);
}); | });
return d.promise; /* or promise() */ | });
} | }
Run Code Online (Sandbox Code Playgroud)
有人告诉我这个被称为" 延迟反模式 "或" Promise构造函数反模式 ",这个代码有什么不好,为什么这被称为 …
我正在使用该async.eachLimit功能一次控制最大操作数.
const { eachLimit } = require("async");
function myFunction() {
return new Promise(async (resolve, reject) => {
eachLimit((await getAsyncArray), 500, (item, callback) => {
// do other things that use native promises.
}, (error) => {
if (error) return reject(error);
// resolve here passing the next value.
});
});
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我无法将该myFunction函数声明为异步,因为我无法访问eachLimit函数的第二个回调内的值.
假设我有一些像这样的asnyc可迭代对象:
// Promisified sleep function
const sleep = ms => new Promise((resolve, reject) => {
setTimeout(() => resolve(ms), ms);
});
const a = {
[Symbol.asyncIterator]: async function * () {
yield 'a';
await sleep(1000);
yield 'b';
await sleep(2000);
yield 'c';
},
};
const b = {
[Symbol.asyncIterator]: async function * () {
await sleep(6000);
yield 'i';
yield 'j';
await sleep(2000);
yield 'k';
},
};
const c = {
[Symbol.asyncIterator]: async function * () {
yield 'x';
await sleep(2000);
yield 'y'; …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用Typescript,但我现在对如何有效地使用async/await感到困惑.
我正在将一堆记录插入到数据库中,我需要获取每个插入返回的ID列表.以下简化示例通常起作用,但它并不像我想的那么优雅,而且完全是顺序的.
async function generatePersons() {
const names = generateNames(firstNames, lastNames);
let ids = []
for (let name of names) {
const id = await db("persons").insert({
first_name: name.firstName,
last_name: name.lastName,
}).returning('id');
ids.push(id[0])
}
return ids
}
Run Code Online (Sandbox Code Playgroud)
我试图用来map避免ids手动创建列表,但我可以让它工作.
我还想拥有的是有限的并行性.所以我的异步调用应该并行发生,直到某个限制,例如,我只想要有10个开放请求,但不是更多.
在Typescript或Javascript ES7中使用async/await实现这种有限的并行性是否有一种相当优雅的方式?或者我是否试图让这个功能做一些不适合的事情?
PS:我知道有数据库的批量插入方法,这个例子有点人为,因为我可以使用它来解决这个特定的问题.但它让我想知道我没有预定义批量方法的一般情况,例如网络请求
我正在寻找一个promise函数包装器,它可以在给定的promise运行时限制/限制,以便在给定的时间只运行一定数量的promise.
在下面的情况下delayPromise,永远不应该同时运行,它们应该以先来先服务的顺序一次运行一个.
import Promise from 'bluebird'
function _delayPromise (seconds, str) {
console.log(str)
return Promise.delay(seconds)
}
let delayPromise = limitConcurrency(_delayPromise, 1)
async function a() {
await delayPromise(100, "a:a")
await delayPromise(100, "a:b")
await delayPromise(100, "a:c")
}
async function b() {
await delayPromise(100, "b:a")
await delayPromise(100, "b:b")
await delayPromise(100, "b:c")
}
a().then(() => console.log('done'))
b().then(() => console.log('done'))
Run Code Online (Sandbox Code Playgroud)
关于如何获得像这样的队列设置的任何想法?
我有精彩的"去抖"功能Benjamin Gruenbaum.我需要修改它来根据它自己的执行而不是延迟来限制一个承诺.
export function promiseDebounce (fn, delay, count) {
let working = 0
let queue = []
function work () { …Run Code Online (Sandbox Code Playgroud) javascript ×5
async-await ×3
promise ×3
bluebird ×2
node.js ×2
asynchronous ×1
es6-promise ×1
iterator ×1
q ×1
typescript ×1