mercredi 3 mai 2017

Anonymous Promises

So promises is fairly new to me but I love the idea.

Previously..

I have previously used this which simply returns the data only after file is read completely:

function something{
     for (var i = 0; i < files.length; i++) {
         //push to the array
         promises.push(readFile(files[i]));
     }

     //use reduce to create a chain in the order of the promise array
     promises.reduce(function (cur, next) {
         return cur.then(next);
     }, Promise.resolve()).then(function () {
         //all files read and executed!
     }).catch(function (error) {
         //handle potential error
     });
}

function readFileAndAddToMap(file) {
 return new Promise(function (resolve, reject) {
     var reader = new FileReader();
     reader.onload = function (progressEvent) {
         loadGeoJsonString(progressEvent.target.result, file.name);
         resolve();
     }

     reader.onerror = function (error) {
         reject(error);
     }

     reader.readAsText(file);
 });
}

Currently

I want to redo this by using ",then" and ".catch" to handle success and non-successes. In my current solution (Google Maps API for clarification but is irrelevant) I want to promise a function without returning a promise in the original function. This is what I have:

//create infobox object on marker
function createInfobox(lat, lng) {
var pos = new G.LatLng(lat, lng);

promiseThis(dropMarker, pos, 'drop marker')
    .then(promiseThis(getGeocodeResult, pos, 'get address'))
    .catch(reason => {alert(reason)})
    .then(promiseThis(isDeliverable, pos, 'determine deliverable status'))
    .catch(reason => {alert(reason)})
    .then(promiseThis(getStores, pos, 'find stores'))
    .catch(reason => {alert(reason)});
}

//handle promises
function promiseThis(doThis, withThis, task) {
    return new Promise(function (resolve, reject) {
        var result = doThis(withThis);
        if (result) {
            resolve(result);
        } else {
            reject('Unable to ' + task);
        }
    });
}

//drop marker on location
function dropMarker(location) {..} //return object

//get geocode results
function getGeocodeResult(latlng) {..} //return string

In the end I would like to keep the results from each promise in an array and use it later. At the moment the getGeocodeResult just returns 'Unable to get address' immediately (also as a console error, not as an alert)

What do I need to know about promises to make this work?




Aucun commentaire:

Enregistrer un commentaire