Last active
December 18, 2019 14:49
-
-
Save Feroz-Istar/4395a7abcbd995ba1289ef3bd72f8745 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. How to create Promise | |
var promise=promiseTest(mydata); | |
promise.done(function (data){}) | |
function promiseTest(mydata){ | |
var senddata={teamdata:JSON.stringify(mydata)}; | |
return $.post("url",senddata); | |
} | |
2. How to use Promise all | |
let promises = []; | |
for(var i=0;i<10;i++){ | |
var promise=promiseTest(mydata); | |
promises.push(promise) | |
} | |
Promise.all(promises).then(() => {}).catch(error => { }); | |
function promiseTest(mydata){ | |
var senddata={teamdata:JSON.stringify(mydata)}; | |
return $.post("url",senddata); | |
} | |
3. Promise All with skipping one error as normmally in Promise All if one request fail it will not called then function | |
let promises = []; | |
for(var i=0;i<10;i++){ | |
var promise=promiseTest(mydata); | |
promises.push(promise) | |
} | |
Promise.all(promises.map(p => p.catch(error => { console.log(error); return null; }))).then(() => {}).catch(error => { }); | |
function promiseTest(mydata){ | |
var senddata={teamdata:JSON.stringify(mydata)}; | |
return $.post("url",senddata); | |
} | |
4. my jquery deffered object | |
var looper = $.Deferred().resolve(); | |
$.get("get_pipeline_list",function(data){ | |
console.log(data); | |
var sequence = Promise.resolve(); | |
$.when.apply($, $.map(data, function(item, i) { | |
looper = looper.then(function() { | |
// trigger ajax call with item data | |
return getPromise(i); | |
}); | |
return looper; | |
})).then(function() { | |
// run this after all ajax calls have completed | |
console.log('Done!'); | |
}); | |
}); | |
function getPromise(id){ | |
var deferred = $.Deferred(); | |
$.get("timeout?id="+id,function (data){ | |
deferred.resolve(data); | |
console.log(data)}).fail(function (){ | |
deferred.reject(error); | |
}); | |
return deferred.promise(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment