Promises are now supported serverside so you can now write asynchronous code.
Example
https.get.promise({
url: 'https://jsonplaceholder.typicode.com/todos/1'
})
.then(function(response){
log.debug({
title: 'Response',
details: response
});
})
.catch(function onRejected(reason) {
log.debug({
title: 'Invalid Get Request: ',
details: reason
});
})
You can also create your own Promises now like so:
let promise = new Promise((resolve) => {
let response = https.get({url: 'https://jsonplaceholder.typicode.com/todos/1'});
resolve(response.body);
});
promise.then(log.debug); //This calls log.debug and uses whatever data is returned as parameter
Async functions (Async/Await)
Async functions provide a way to structure and simplify your asynchronous code. It is useful to avoid callback that comes with Promises as it can get pretty complex.
Example
async function asyncRequest(){ //<< Note the async keyword next to the function
let promise = new Promise((resolve) => {
let response = https.get({url: 'https://jsonplaceholder.typicode.com/todos/1'});
resolve(response.body);
});
//promise.then(log.debug); //No longer using this
let responseBody = await promise; //Using await instead
log.debug('responseBody', responseBody); //Logs correctly
}