Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Issue Promise.all()

I want to declare an array of empty promises
then resolve them manually, but it is not working ?

const tab = [1]
let promises = tab.map(e => new Promise(() => { }))
setTimeout(() => {
  promises[0] = Promise.resolve()
}, 3000);
Promise.all(promises)
  .then(e => console.log("finished"))

I DO NOT WANT to do it like this :

const tab = [1]
let promises = tab.map(e => new Promise((resolve) => { 
  setTimeout(() => {
    resolve()
  }, 3000);
}))

Promise.all(promises)
  .then(e => console.log("finished"))
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Thats the closest you can get:

const tab = [1]
let resolves = []
let promises = tab.map(e => new Promise((resolve) => {
   resolves.push(resolve)
}))
setTimeout(() => {
  resolves[0]()
}, 3000);
Promise.all(promises).then(e => console.log("finished"))
about 4 years ago · Juan Pablo Isaza Report

0

Here's a technique using a reusable deferred function that allows you to externally control a promise. We extend both resolve and reject functionality, allowing better control for the caller. This is a somewhat unconventional pattern but there are advantages to exploiting promises in this way. See this Q&A for an detailed example -

function deferred () {
  let resolve, reject, promise = new Promise((res, rej) => {
    resolve = res; reject = rej
  })
  return { promise, resolve, reject }
}

const tasks = [ deferred(), deferred(), deferred() ] // 3 "deferrred" objects

Promise.all(tasks.map(t => t.promise)).then(console.log, console.error)

setTimeout(_ => tasks[0].resolve("one"), 1000)
setTimeout(_ => tasks[1].resolve("two"), 2000)
setTimeout(_ => tasks[2].resolve("three"), 3000)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!