I am trying to find some way to write my general promisify function to use async and wait with socket.io
Here is my code the first one fail to finish while the second success without problem.
What is the difference between these two? and why would the first one fail?
function promisify (func) {
return new Promise((resolve, reject) => func(() => {
resolve()
console.log('closeServer')
}))}
async function timer1 () {
await Promise.resolve(promisify(server.close))
}
function closeServer () {
return new Promise((resolve, reject) => server.close(() => {
resolve()
console.log('closeServer')
}))}
async function timer2 () {
await Promise.resolve(closeServer())
}
let me show you guys the log,
the second one
> nodemon src/index.js --exec babel-node --presets es2015
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node src/index.js --presets es2015`
Server listening at port 3000
^C(node:28061) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property '_handle' of undefined
(node:28061) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
the second
> nodemon src/index.js --exec babel-node --presets es2015
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node src/index.js --presets es2015`
Server listening at port 3000
^CcloseServer