javscript async await explained
// Promise approach
function getJSON(){
// To make the function blocking we manually create a Promise.
return new Promise( function(resolve) {
axios.get('https://tutorialzine.com/misc/files/example.json')
.then( function(json) {
// The data from the request is available in a .then block
// We return the result using resolve.
resolve(json);
});
});
}
// Async/Await approach
// The async keyword will automatically create a new Promise and return it.
async function getJSONAsync(){
// The await keyword saves us from having to write a .then() block.
let json = await axios.get('https://tutorialzine.com/misc/files/example.json');
// The result of the GET request is available in the json variable.
// We return it just like in a regular synchronous function.
return json;
}
Also in JavaScript:
- fullscreen electron
- TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined at validateString (internal/validators.js:120:11)
- select jquery display none
- remove last 3 characters from string javascript
- datatable dropdown toggle not working
- return an array of strings from an array of objects js
- react hooks call child fuynction
- open new tab with javascript
- mongoose
- break loop after time javascript
- stop propagation
- how to draw a triangle using javascript
- react ntive
- javascript on script loaded
- dockerfile for nodejs
- how to remove spaces in javascript
- javascript pass iterator to callback
- trigger click on checkbox jquery
- how to delete element in list javascript
- javascript get all options from select
- variables in js class
- js add src to img
- nodejs copy and rename file
- how to use media queries in emotion