相关疑难解决方法(0)

在新的Promise()构造函数中使用async/await是一种反模式吗?

我正在使用该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函数的第二个回调内的值.

javascript asynchronous node.js async-await

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

添加延迟到返回promise的javascript方法

我目前正在尝试学习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)

timeout settimeout promise typescript angular

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