jquery-deferred

jQuery $.wait() for *all* deferred's to be rejected?

假如想象 提交于 2019-12-01 06:17:32
问题 jQuery has a nice feature of its Deferred API, $.wait() for working with multiple Deferred s / Promise s. It returns when: All of the Deferred s have been resolve() d or One of the Deferred s has been reject() ed Most of the time this is what you want, but sometimes you want to know when all of them have been reject() ed. Is there a simple or elegant way to do something like $.wait() but only when all Deferred s have been rejected? (There may be other use cases but mine is to combine this

Loop with each iteration only happening after jQuery deferred when/then possible without recursion?

一个人想着一个人 提交于 2019-12-01 04:53:34
问题 I want to call jQuery deferred functions in a loop but each iteration should wait for the previous iteration to complete using the deferred when() function (num_of_iterations) { var arr = []; for (var i = 1; i < num_of_iterations; ++i) { arr.push($.getJSON( ... 1 ... )); arr.push($.getJSON( ... 2 ... )); ... $.when.apply($, arr).then(function() { // somehow do the next iter. only now that all the asynch getJSON's are done }); } return; } Now of course since getJSON is asynchronous all the

Conjuring JQuery Deferred with monadic incantations

我们两清 提交于 2019-12-01 04:12:31
问题 Inspired by this (excellent) discussion of using Promises in javascript, I'm trying to work out how I could use Deferred to chain together async and non-async functions, to avoid paying the callback tax when using my 'Global storage' code. I've got a few questions related to this, but I'll ask them together here, because the context is the same. One thing I can't work out is how I can make a deferred out of something that isn't asynchronous - that is, how do I take a value, wrap it in a

Is it possible to reset a jQuery deferred object state?

我是研究僧i 提交于 2019-12-01 03:09:09
Is it possible to reset a resolved jQuery object to an 'unresolved' state and kick off it's initialization and callbacks all over again? The specific thing I'm doing is that I have a jQuery deferred wrapper over the local file system api. From there I build up higher level deferreds for the things I care about: var getFs = defFs.requestQuota(PERSISTENT, 1024*1024) .pipe (bytes) -> defFs.requestFs(PERSISTENT, bytes) var getCacheContents = getFs.pipe (fileSystem) -> defFs.getDirectory('Cache', fileSystem.root).pipe (dir) -> defFs.readEntries(dir) Now most of the time, when I call

Wait for the end of an asynchronous Javascript function to retrieve a result (Deferred ?)

限于喜欢 提交于 2019-12-01 01:55:56
Ok guys, I know this topic has already been discussed multiple times, but I can't find an answer to solve my problem. So I'm trying to do a simple think : I'm creating a string, like : distance is : " + CalculateDistance(position); The wanted result is something like distance is 5kms (8min) . CalculateDistance(position) is a function calling a Google maps API called DistanceMatrix to calculate distance between two points. The API is documented here , and the given sample perfectly works. I adapted it like this : function CalculateDistance(position) { var service = new google.maps

Is it possible to reset a jQuery deferred object state?

纵饮孤独 提交于 2019-11-30 23:10:57
问题 Is it possible to reset a resolved jQuery object to an 'unresolved' state and kick off it's initialization and callbacks all over again? The specific thing I'm doing is that I have a jQuery deferred wrapper over the local file system api. From there I build up higher level deferreds for the things I care about: var getFs = defFs.requestQuota(PERSISTENT, 1024*1024) .pipe (bytes) -> defFs.requestFs(PERSISTENT, bytes) var getCacheContents = getFs.pipe (fileSystem) -> defFs.getDirectory('Cache',

jQuery ajax call chaining with multiple dependencies

荒凉一梦 提交于 2019-11-30 19:17:43
问题 I don't quite understand magic deferred objects with jQuery. Assume the following code: function callWebService(uri, filter, callback) { var data = {}; if (filter && filter != '') data['$filter'] = filter; jQuery.ajax({ url: '/_api/lists/' + uri + '/items', data: data, success: callback, dataType: 'json' }); } function getInitialData() { callWebService("InitialData", "", function (data) { //do stuff with data }); } function getGreenData() { callWebService("GreenData", "filter from InitialData

Chain of Jquery Promises

偶尔善良 提交于 2019-11-30 11:56:24
I have a simple chain of events: Get Columns from a metaData table (async) load selected columns (async) render list I used to just the chain these functions, each calling the next when it had completed. However, its not very obvious what's going (calling getColumnsFromMeta results in the view being populated). So in the interest of clarity and code re-use I'd like to refactor these using JQuery Promises . I have used promises before. But how do I chain more than two? getColumnsFromMeta ().then(loadSourceFromDatabase /*some arguments*/) //.then(renderList)?; Here's an example of the

Programmatically creating a chained sequence of jQuery promises

房东的猫 提交于 2019-11-30 10:51:30
I have a function to delete a list an array of files, filePaths, by calling a single deleteFile(filePath) on each of the files in a list (some APIS I'm using don't support bulk delete). The function deleteFile return a jQuery promise and resolves/rejects upon deleting the file. function deleteFiles(filePaths) var deferreds = $.map(fileKeys, function (val, index) { return deleteFile(filePath); }); $.when.apply($, deferreds).then(function (schemas) { console.log("DONE", this, schemas); deferred.resolve(); }, function (error) { console.log("My ajax failed"); deferred.reject(error); }); I am

jQuery Deferred: Rejecting a Promise from within a Done Filter

浪子不回头ぞ 提交于 2019-11-30 06:35:42
Fyi, I'm just starting to learn jQuery promises, so I may be a bit confused here. Anyway, I have an AJAX request that I want to reject from within a done filter based on the content of the response: return doAJAXRequest().then(function (data) { if (data.responseText == "YES") { return doOtherAJAXRequest(); } else { this.reject(data); } }); That didn't work as I expected: Uncaught TypeError: Object #<Object> has no method 'reject' How do I make this promise fail based on its response? Is that possible? Or am I just confused about what needs to be done here? In jQuery (unlike some other libs),