I'm having a hard time wrapping my head around JQuery promises and deferred objects so please bear with me. I should also say my application is using React, Typescript, and ES6.
Let's say I have an array of objects:
[{ Object }, { Object}, { Object }]
What I'm trying to do is, for each object in the array, make a call out to an API with a different parameter from the object, get the response, and then make another call to the same API for the remaining objects. Basically, I want to chain these calls together so I make one at a time and then add them into my application state.
Here's what I've got so far but it's, obviously, not working:
private getData(options: any[]): void {
let promises: any[] = [];
options.map((option: any, key: number) => {
let deferred: JQueryDeferred<any> = $.Deferred();
deferred.done((response) => {
return this.getIndividual(option)
.done(response => {
console.log('done', response);
});
});
promises.push(deferred);
});
$.when.apply($, promises)
.then((response) => {
console.log('response', response);
}).fail((error) => {
console.log("Ajax failed");
})
.done(() => {
console.log('done');
});
}
private getIndividual(option: any) {
return apiCall(option.hashKey);
}