Are JS Promises, processor-like parallelism?

前端 未结 2 875
有刺的猬
有刺的猬 2021-01-27 19:38

I was reading about the JS Promises and since they are generally

extremely useful for async success/failure, because you\'re less interested in the exact

2条回答
  •  悲哀的现实
    2021-01-27 19:58

    No, this is not parallel processing as promises can be on a single thread and there is no concept of task parallelization built into them, they arent going to break a problem down into parallel subtasks etc. They are solely a method to respond to and manage asynchronous events.

    The best mock I could think of to mimic parallelism would require the user to break their problem down into parallizable tasks and then pass each call to a Promise.All type function which would use webworkers to handle the invocations. Most likely a useless exercise as you are basically just decorating webworkers with promises. You'd be much better advised to leverage a library like parallel.js in cases like this.

提交回复
热议问题