sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.45k stars 185 forks source link

Using p-queue with `require` #143

Closed fergiemcdowall closed 3 years ago

fergiemcdowall commented 3 years ago

My node project doesn't let me import modules. Is it possible to use p-queue with require? If so, what is the recommended incantation?

sindresorhus commented 3 years ago

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

fergiemcdowall commented 3 years ago

👍

fergiemcdowall commented 3 years ago

If anybody stumbles upon this later, the incantation for those forced to use require is ->

const queue = new (await import('p-queue')).default({concurrency: 1})
otanim commented 3 years ago

Thanks for the answer @fergiemcdowall, much appreciated!

Just in case if anyone wants to re-use the PQueue constructor instead of reusing the same long line of an importation:

let PQueue;
(async () => {
    PQueue = (await import('p-queue')).default;
})();

//... * anything *

const queue1 = new PQueue({concurrency: 5});

const queue2 = new PQueue({concurrency: 10});
fergiemcdowall commented 3 years ago

@otanim yes, if you account for the possible race condition when you first assign PQueue, then thats a nice way to do it 👍

gabor-petho commented 3 years ago

If anybody stumbles upon this later, the incantation for those forced to use require is ->

const queue = new (await import('p-queue')).default({concurrency: 1})

I got

const queue = new (await import('p-queue')).default({ concurrency: 1 });
                   ^^^^^
SyntaxError: Unexpected reserved word

error for this

LeadDreamer commented 3 years ago

That would be because OP used the await INSIDE an Async Closure. Your example appears to use the await AT TOP LEVEL, which doesn't work in any except possibly a beta version of Node.

If you don't know what Top Level, Async, Await and Closure actually mean you have a fair bit of studying to do before you'll be able to use this.

8427003 commented 2 years ago

why so trick do this, some babel ts project can't use this package