NodeJS Timeout a Promise if failed to complete in time

前端 未结 7 542
忘掉有多难
忘掉有多难 2020-11-27 04:07

How can I timeout a promise after certain amount of time? I know Q has a promise timeout, but I\'m using native NodeJS promises and they don\'t have .timeout function.

7条回答
  •  死守一世寂寞
    2020-11-27 04:27

    If your code is placed in a class you could use a decorator for that. You have such decorator in the utils-decorators (npm install --save utils-decorators):

    import {timeout} from 'utils-decorators';
    
    class SomeService {
    
       @timeout(3000)
       doSomeAsync(): Promise {
        ....
       }
    }
    

    https://github.com/vlio20/utils-decorators#timeout-method

提交回复
热议问题