You can do something like this
// @flow
const toPromise = (f: (any) => void) => {
return new Promise<any>((resolve, reject) => {
try {
f((result) => {
resolve(result)
})
} catch (e) {
reject(e)
}
})
}
export default toPromise
Then use it
async loadData() {
const friends = await toPromise(FriendsManager.loadFriends)
console.log(friends)
}
manpreet
Best Answer
2 years ago
I want to work with promises but I have a callback API in a format like:
1. DOM load or other one time event:
2. Plain callback:
3. Node style callback ("nodeback"):
4. A whole library with node style callbacks:
How do I work with the API in promises, how do I "promisify" it?