http://itsnull.com/presentations/js-async/
Created by Kip Streithorst / @itsnull
function getClosestStore(zipCode) {
var storeIds = getNearbyStores(zipCode);
if (storeIds.length) {
return getStoreDetails(storeIds[0]);
}
return null;
}
function getClosestStore(zipCode, successCallback, failureCallback) {
getNearbyStores(zipCode, function (storeIds) {
if (storeIds.length) {
getStoreDetails(storeIds[0], function (storeDetails) {
successCallback(storeDetails);
}, failureCallback);
} else {
successCallback(null);
}
}, failureCallback);
}
var retData = performOperation();
//... code that comes after
//becomes
performOperation(function (retData) {
//.. code that comes after
})
function getClosestStore(zipCode) {
var retValue = new Future();
getNearbyStores(zipCode).done(function (storeIds) {
if (storeIds.length) {
getStoreDetails(storeIds[0]).done(function (storeDetails) {
retValue.resolve(storeDetails);
}).fail(retValue.reject);
} else {
retValue.resolve(null);
}
}).fail(retValue.reject);
return retValue;
}
function getClosestStore(zipCode) {
return getNearbyStores(zipCode).then(function (storeIds) {
if (storeIds.length) {
return getStoreDetails(storeIds[0]);
}
return null;
});
}
var initialData = await getInitialData();
var result = await parseData(initialData);
printResult(result);
function async getClosestStore(zipCode) {
var storeList = await getNearbyStores(zipCode);
if (storeList.length) {
return await getStoreDetails(storeList[0]);
}
return null;
}
function getClosestStore(zipCode) {
var storeList = getNearbyStores(zipCode);
if (storeList.length) {
return getStoreDetails(storeList[0]);
}
return null;
}
function async getClosestStore(zipCode) {
var storeList = await getNearbyStores(zipCode);
if (storeList.length) {
return await getStoreDetails(storeList[0]);
}
return null;
}
getClosestStore(12121).then(function (storeDetails) {
if (store.open) { //has an exception?
//do stuff
}
});
fetch('/api/users').then(function (response) {
//processing
});
fetch('/api/users', {method: 'POST', body: new FormData(form)});