Not sure if this is what you're referring to when you say
but the promise executor function needs to use await to get some data
but it sounds like you're dealing with some asynchronous call inside of a promise that you need to "wait" for, then resolve
with the data that was returned as a result.
So for example,
EDIT: As pointed by @Bergi in the comments below, you probably shouldn't be doing this (wrapping a promise inside of another promise):
// Assuming you're making an API with `axios` to grab some data
const promise = new Promise((resolve, reject) => {
axios.get('www.example.com/api', {...})
.then(result => {
resolve(result)
})
.catch(error => {
reject(error)
})
})
const callback = (data) => {
console.log('data', data)
}
// `callback` will get called when the `axios.get` "finishes" (resolves)
// with some data after it's done fetching, no need to `await` here
promise.then(callback)
Instead, you could do promise chaining, if needed:
// using the same example from above
const handleResponse = (result) => {
// handle response
}
const handleError = (error) => {
// handle error
}
axios.get('www.example.com/api', {...})
.then(handleResponse)
.then(doSomethingElse)
.catch(handleError)
// or, if you need to make multiple asynchronous calls use `Promise.all`
const handleResponses = (response) => {
const [users, books] = response
// do the rest
}
Promise.all([
axios.get('www.example.com/api/users'),
axios.get('www.example.com/api/books')
])
.then(handleAPIResponses)
.then(doSomethingElse)
.catch(handleError)
Similarly, if you're dealing with the "error first" callback pattern
// `fs.readFile` with Node.js
const promise = new Promise((resolve, reject) => {
fs.readFile('...', (err, data) => {
if (err) {
reject(err)
return
}
resolve(data)
})
})
const callback = (data) => {
console.log('data', data)
}
// again, `callback` will get called when reading a file is done
// and you get the result back
promise.then(callback)
If you need to make multiple calls inside of a promise
and then resolve with the final value, you could do something like the following:
async function promise() {
try {
// assuming API.getUserIds() returns a promise
const userIds = await API.getUserIds(...)
const users = await API.getUsers(userIds)
return users
} catch (err) {
// ...
}
}
const callback = (data) => {
console.log('data', data)
}
// again, `callback` will get called when both API requests
// resolve with some values and when the above function returns `users`
promise().then(callback)
Personally, I'd stay away from #2
, and whatever you're trying to accomplish with that pattern (depending on your case) can be easily done choosing one of the examples above.