Getting error TypeError: Promise.any is not a function

Viewed 8142

I am learning Promise.any from the following site. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any The IDE does not show any error, but while running using yarn command , I am getting the following error.

E:\typescript-2020-1\promise-usages-1\lib\basics1\promise-any.service.js:18                                                
        Promise.any([                                                                                                      
                ^                                                                                                                                                                              
TypeError: Promise.any is not a function                                                                                   
    at PromiseAnyService.validateAll_Type1 (E:\typescript-2020-1\promise-usages-1\lib\basics1\promise-any.service.js:18:17)
    at Object.<anonymous> (E:\typescript-2020-1\promise-usages-1\lib\test.js:35:7)                                         
    at Module._compile (internal/modules/cjs/loader.js:1137:30)                                                            
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)                                              
    at Module.load (internal/modules/cjs/loader.js:985:32)                                                                 
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)                                                       
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)                                    
    at internal/main/run_main_module.js:17:47                                                                              
 ERROR  Command failed with exit code 1.      

                                                                         

I have written the simple code to test. Please help where I am doing wrong in a Typescript class.

const promise1 = Promise.reject(0);
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick'));
const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow'));

const proms = [promise1, promise2, promise3];

Promise.any(proms).then((result) => console.log(result));

Currently I am using NodeJs 12 and Typescript 3.8.3

2 Answers

The method Promise.any was supported in node.js 15.0.0, your node.js version is old, so you could update it and try again.

Imho it's better to stick with whatever is supported in your current setup, and it seems like esnext.promise is not there yet, but:

I'm in a situation where I'm porting a sizeable live codebase away from Bluebird, and this npm module did it for me (I'm on node v12.12.0): https://www.npmjs.com/package/promise.any

Related