Created
February 8, 2015 16:55
-
-
Save danharper/74a5102363fbd85f6b67 to your computer and use it in GitHub Desktop.
ES7's async/await syntax.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES7, async/await | |
function sleep(ms = 0) { | |
return new Promise(r => setTimeout(r, ms)); | |
} | |
(async () => { | |
console.log('a'); | |
await sleep(1000); | |
console.log('b'); | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES6, native Promises, arrow functions, default arguments | |
function sleep(ms = 0) { | |
return new Promise(r => setTimeout(r, ms)); | |
} | |
console.log('a'); | |
sleep(1000).then(() => { | |
console.log('b'); | |
}); | |
// or if we were to be strictly identical, an "async" function returns a | |
// Promise, so this is more accurate, but the fluff isn't needed for in this case | |
(function() { | |
return new Promise((resolve, reject) => { | |
sleep(1000).then(() => { | |
console.log('b'); | |
resolve(); | |
}); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES6, native Promises ONLY | |
function sleep(ms) { | |
ms = ms || 0; | |
return new Promise(function(resolve) { | |
setTimeout(resolve, ms); | |
}); | |
} | |
(function() { | |
console.log('a'); | |
sleep(1000).then(function() { | |
console.log('b'); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES5, nothing special | |
function sleep(ms, callback) { | |
ms = ms || 0; | |
setTimeout(callback, ms); | |
} | |
(function() { | |
console.log('a'); | |
sleep(1000, function() { | |
console.log('b'); | |
}); | |
})(); |
Thanks!
Do we need the reject callback inside a promise?
The below code seems to be working out for me, it catches any error and instead of rejecting it returns null in the resolve function.
Is this bad code, if so why?
function fecthDB() {
return new Promise((resolve, reject) => {
try {
db.query('SELECT * FROM users' (err, result) => {
if (err || !results) {
return resolve(null)
}
resolve(result)
})
} catch (e) {
resolve(null)
}
})
}
function async getUsers() {
var users = await fecthDB()
if (!users) { // error handling
return console.log('error')
}
console.log(users) // got users
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it seems that in React Native, I can use 1-sleep-es7.js.