You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Now, if the sub functionsgetToken,getSecret,getUserIds becomes async (all return Promise),the only change we need to make is "lifting"getUserIds withliftp.
// Promise [UserId]varuserIdsP=P.liftp(getUserIds)(tokenP,secretP);// [Photo] -> Photo -> [Photo]varappendPhotos=function(photos,photo){returnphotos.concat([photo]);};// Promise [Photo]varphotosP=P.foldp(function(photos,userId){// Promise PhotovarphotoP=getPhotoByUserId(userId);returnP.liftp(appendPhotos)(P.purep(photos),photoP);// `P.purep` is equivalent to `Promise.resolve`})([])(userIdsP);photosP.then(console.log);// [":)", ":D", ":-|"]
The above code will fetch photo by userId sequentially. If it fails to fetch the first photo,it will reject the promise without fetching next photo.And it will resolve the promise once all the photos have been fetched.
Wait until the second async call to finish, then return the value of the first async call
Let's say we want to send an email with all the photos, and wait until the email has been sent,then resolve the promise with the photos
Withfirstp, we can wait until the email has been sent, and return the result of photos whichis from the first promise.
varsendEmailWithPhotos=function(photos){returnPromise.resolve('The email has been sent');};// Promise [Photo]varphotosP=P.foldp(function(photos,userId){// Promise PhotovarphotoP=getPhotoByUserId(userId);returnP.liftp(appendPhotos)(P.purep(photos),photoP);// `P.purep` is equivalent to `Promise.resolve`})([])(userIdsP);// Promise StringvarsentP=P.liftp1(sendEmailWithPhotos)(photosP);// ^^^^^^^^ P.liftp1 is equivalent to P.liftp when there is only one promise to resolve.// But P.liftp1 has better performance than P.liftp.P.first(photosP,sentP).then(console.log);// [":)", ":D", ":-|"]
Takes a Promise, returns a Promise that resolves with undefined when the input promise is resolved,or reject with the same error when the input promise is rejected.
Maps a function that takes a value a and returns a Promise of value b over an array of value a,then usesequencep to transform the array of Promise b into a Promise of array b
Takes a function so that this function is able to read input values from resolved Promises,and return a Promise that will resolve with the output value of that function.
Returns a Promise of value b by iterating over an array of value a, successivelycalling the iterator function and passing it an accumulator value of value b,and the current value from the array, and then waiting until the promise resolved,then passing the result to the next call.
Builds a list from a seed value. Accepts an iterator function, which takes the seed and return a Promise.If the Promise resolves to afalse value, it will return the list.If the Promise resolves to a pair, the first item will be appended to the list and the second item will be used as the new seed in the next call to the iterator function.
Takes apredict function and atoError function, return a curriedfunction that can take a value and return a Promise.If this value passes the predict, then return a resolved Promise withthat value, otherwise pass the value to thetoError function, andreturn a rejected Promise with the output of thetoError function.
varvalidateGreaterThan0=toPromise(function(a){returna>0;},function(a){returnnewError('value is not greater than 0');});>validateGreaterThan0(10)promise10>validateGreaterThan0(-10)rejectedpromiseError'value is not greater than 0'
Takes two functions and return a function that can take a Promise and return a Promise.If the received Promise is resolved, the first function will be used to map over the resolved value;If the received Promise is rejected, the second function will be used to map over the Error.
Like Promise.prototype.then function, but takes functions first.
varadd1OrReject=bimap(function(n){returnn+1;},function(error){returnnewError('can not add value for '+error.message);});>add1OrReject(P.purep(2))promise3>add1OrReject(Promise.reject(newError('NaN')));promise'can not add value for NaN'
About
A practical functional programming library for promises