我正在使用该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函数的第二个回调内的值.
我目前正在尝试学习Angular 2,打字稿,承诺等.我为开发人员工具和只返回硬编码数据的服务设置了一个小应用程序.这仅用于测试目的.
我想在服务方法上添加短暂超时来模拟服务器滞后以测试我的一些控件,但是我找不到正确的语法来执行此操作.如何为服务电话添加5秒延迟?
开发者工具服务
@Injectable()
export class DeveloperService {
getExampleData(): Promise<ExampleItem[]> {
const examples: ExampleItem[] = [];
examples.push({ id: 1, name: 'Spaceman Spiff', location: 'Outer Space', age: 12 });
examples.push({ id: 2, name: 'Stupendous Man', location: 'The City', age: 30.5 });
examples.push({ id: 3, name: 'Tracer Bullet', location: 'The City', age: 24 });
examples.push({ id: 4, name: 'Napalm Man', location: 'War Zone', age: 43.333 });
examples.push({ id: 5, name: 'Adult Calvin', location: 'In the future', age: 54 });
// TODO: …Run Code Online (Sandbox Code Playgroud) angular ×1
async-await ×1
asynchronous ×1
javascript ×1
node.js ×1
promise ×1
settimeout ×1
timeout ×1
typescript ×1