jQuery deferred promise progress notification - promise

I've been playing with promises and trying to build some sort of progress notification.
The code is executing all functions in the right order, but the progress updates execute just before the resolve as opposed to when they actually happen.
Can anyone point out what I'm doing wrong?
function start(x) {
console.log("Start: " + x);
var promise = process(x);
console.log("promise returned");
promise.then(function(data) {
console.log("Completed: " + data);
}, function(data) {
console.log("Cancelled: " + data);
}, function(data) {
console.log("In Progress: " + data);
});
}
function process(x) {
var deferred = $.Deferred();
var promise = deferred.promise();
// process asynchronously
setTimeout(function() {
for (var i=0 ; i<x ; i++) {
sleep(1000);
deferred.notify(i);
}
if (x % 2 === 0) {
deferred.reject(x);
} else {
deferred.resolve(x);
}
}, 0);
return promise;
}
function sleep(sleepDuration) {
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}
start(3);
Fiddle here:
https://jsfiddle.net/n86mr9tL/

A delay timer implemented with while(), will "block" - ie hog the processor.
Blocking not only prevents other javascript from running but also inhibits reliable refreshing of the browser screen including the console. So whereas those deferred.notify(i) and console.log("In Progress: " + data) statements are firing, the console isn't refreshed until the processor becomes free to do so.
Unsurprisingly, the solution lies in not using while().
Fortunately, javascript imcludes two built-in methods window.setTimeout() and window.setInterval(), which are conceptually different from a while() idler but fulfil the same role .... without blocking.
window.setInterval(fn, t) fires function fn every t milliseconds,
window.setTimeout(fn, t) fires function fn once, after t milliseconds.
Both methods return an opaque reference, which allows them to be cancelled.
In the code below, start() is unmodified, process() is heavily modified and sleep() has disappeared.
process() now does the following :
creates a jQuery Deferred and returns a promise derived from it,
establises a setInterval() of 1000 milliseconds (1 second), whose function :
keeps count of how many times it has been called,
calls deferred.notify() every second until the counter i reaches the specified maximum x,
when the specified maximum is reached :
the interval, which would otherwise silently tick away ad infinitum, is cleared,
deferred.resolve() or deferred.reject() are called to settle the Deferred (and its promise),
function start(x) {
console.log("Start: " + x);
process(x).then(function(data) {
console.log("Completed: " + data);
}, function(data) {
console.log("Cancelled: " + data);
}, function(data) {
console.log("In Progress: " + data);
});
}
function process(x) {
return $.Deferred(function(dfd) {
var i = 1;
var intervalRef = setInterval(function() {
if(i < x) {
dfd.notify(i++);
} else {
clearInterval(intervalRef);
dfd[(x % 2 === 0)?'reject':'resolve'](x);
}
}, 1000);
}).promise();
}
console.clear();
start(3);
Updated fiddle

Related

I tried to maka a QUnit async test for checking ajax update

I tried to maka a QUnit async test for checking ajax update.
I read of QUnit.asyncTest here
https://www.sitepoint.com/test-asynchronous-code-qunit/
but if i try this i get a
TypeError: QUnit.asyncTest is not a function
thats the complete source: https://gist.github.com/232457b002e5363439aece7535600356
of course i new by using QUnit and used JavaScript not for long time.
that a snippet of the part where the error happens:
function max() {
var max = -Infinity;
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
// https://www.sitepoint.com/test-asynchronous-code-qunit/
//TypeError: QUnit.asyncTest is not a function
QUnit.asyncTest('max', function (assert) {
expect(1);
window.setTimeout(function() {
assert.strictEqual(max(3, 1, 2), 3, 'All positive numbers');
QUnit.start();
}, 0);
});
this test gives no syntax error but gives old date:
QUnit.test('usersInnerHTMLlength_Is24', function(assert) {
// problem: this not reads the updates done by ajax. means that are old data:
let innerHTMLlength = $("#users").html().toString().length;
assert.equal(innerHTMLlength, 24);
});
May its not possible to check ajax with QUnit?
I thougt this when i have read here:
QUnit testing AJAX calls
I use it inside a Wordpress Plugin
That sitepoint article is very old (by web standards). You'll need to use the newer syntax found on the documentation website:
function someAsyncThing(value) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
if (value > 5) {
resolve(value);
} else {
reject(new Error("bad value"));
}
}, 500);
});
}
QUnit.test( "some async thing success test", function( assert ) {
// This is what tells QUnit the test is asynchronous
// "done" here will be a callback function used later
var done = assert.async();
// Now call your Async code, passing in a callback...
someAsyncThing(10)
.then(function(result) {
// do your assertions once the async function ends...
assert.equal(result, 10)
// Now tell QUnit you're all done with the test
done();
})
// we can pass the "done" callback from above into catch() to force a test failure
.catch(done);
});
QUnit.test( "some async thing FAILURE test", function( assert ) {
var done = assert.async();
someAsyncThing(4)
.then(function() {
done(new Error("we should NOT succeed with a value < 5"));
})
.catch(function(err) {
assert.equal(err.message, "bad value")
});
});

can't execute query.find() in my cloud code

I have a cloud code that fetch data from 'Core' & HTTP API and do some logic on it. Now I want to send push notification based on the result but the code doesn't even getting executed here is a snippet of code
.then(function(result) {
for (var i = 0; i < queryObjects.length; i++) {
var object = queryObjects[i];
console.log('This will loop for '+queryObjects.length+' times');
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo('installationID',object.get('installationID'));
pushQuery.equalTo('deviceType','ios');
Parse.Push.send({
where: pushQuery,
data: {
alert: "When do we take to outer space",
badge: "Increment"
}
}, {
success: function() {
console.log("Push was successful");
},
error: function(error) {
console.error(error);
}
});
}
Even If I tried to execute query.find() nothing happened
.then(function(result) {
for (var i = 0; i < queryObjects.length; i++) {
var object = queryObjects[i];
console.log('This will loop for '+queryObjects.length+' times');
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo('installationID',object.get('installationID'));
pushQuery.equalTo('deviceType','ios');
pushQuery.find({
success:function(list) {
console.log("Query Data: "+list);
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
}
});
}
This is because of the asynchronous nature of Push.Send function. You are calling it multiple times in a loop but you do not wait for any of those calls to finish before returning from the .then block. You need to know that when a Pasre asynchronous function returns, it is not necessarily finished. You need to wait on its Promise to be fulfilled just to make sure it has completed.
You can change your code to make sure that you call Parse.Push function only once (in your Installation query, use containedIn constraint on the array of your installationIDs you build in the loop and call Parse.Push only once after the loop and make sure to return its Promise.
The other solution is that for each time you call a Parse.Push function in the loop, you need to push them in an array of promises and then wait for all of them to finish using Parse.Promise.when(promises);. The documentation is very clear on how to wait on Promises in parallel.

d3.js/queue.js: How to skip errors in queue.js

I'm working with the last.fm API and queue.js to dynamically merge a user's Weekly Track List and Track Info datasets together. I'm running into a problem when my AJAX call for a certain song returns an error. My main question is, how do I skip these errors? Specifically, how do I alter Mike Bostock's taskThatSometimesFails function to continue onto the next d3.json call?
Here's my original code:
d3.json(top, function(json) {
weeklyChart = json.weeklytrackchart.track;
var q = queue()
.defer(d3.json, top);
weeklyChart.forEach(function(d) {
tracks = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=...&artist='+ d.artist['#text'] + '&track=' + d.name + '&format=json';
q.defer(d3.json, tracks);
});
q.awaitAll(function(error) {
if (error) throw error;
console.log(arguments);
Here's Mike Bostock's suggested way of skipping over error/null responses:
queue(1)
.defer(ignoreError, taskThatSometimesFails)
.defer(ignoreError, taskThatSometimesFails)
.awaitAll(function(error, results) {
if (error) throw error; // never happens
console.log(results); // e.g., [0.23068457026965916, undefined]
});
function ignoreError(task, callback) {
task(function(error, result) {
return callback(null, result); // ignore error
});
}
function taskThatSometimesFails(callback) {
setTimeout(function() {
var x = Math.random();
if (x > .5) return callback(new Error("failed"));
callback(null, x);
}, 250);
}
I don't know node.js, and am pretty confused by how callback and setTimeout work. I tried adapting Mike's code to my situation, but I'm getting stuck on what to do with taskThatSometimesFails (where the magic is, I suppose). Here's my current, crappy adaptation:
d3.json(top, function(json) {
weeklyChart = json.weeklytrackchart.track;
var q = queue()
.defer(d3.json, top);
weeklyChart.forEach(function(d) {
tracks = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=...&artist='+ d.artist['#text'] + '&track=' + d.name + '&format=json';
q.defer(d3.json, tracks);
});
function ignoreError(task, callback) {
task(function(error, result) {
return callback(null, result);
});
}
function taskThatSometimesFails(callback) {
setTimeout(function() {
if (error) return callback(new Error("failed"));
return callback(null, tracks); //or maybe return d3.json(tracks)?
}, 1000);
}
q.awaitAll(function(error) {
if (error) throw error;
For now, any null responses are still breaking the whole queue. Any help, as always, appreciated. Thanks, guys.

Parse : Trying multiple query in a function of Parse Cloud code. Second query doesn't seem to work

I am trying to run multiple queries inside Parse cloud function. Second query doesnt get
executed
Parse.Cloud.beforeSave("PhoneNumbers", function(request, response) {
// Check if the same phone number for the same user exists
var PhoneNumbersObject = Parse.Object.extend('PhoneNumbers');
var duplicationQuery = new Parse.Query(PhoneNumbersObject);
duplicationQuery.equalTo('user', Parse.User.current());
duplicationQuery.equalTo('phoneNumber', request.object.get("phoneNumber"));
duplicationQuery.count({
success: function(count) {
if (count > 0) {
response.error("This number is already stored");
}
},
error: function(error) {
response.error("Error " + error.code + " : " + error.message + " when storing phone number");
}
});
var firstPhoneNumberQuery = new Parse.Query(PhoneNumbersObject);
firstPhoneNumberQuery.equalTo('user', Parse.User.current());
firstPhoneNumberQuery.find({ // <<<<< find doesn't work
success: function(results) {
if ( results.length == 0 ) {
console.log("User's first telephone number"); // <<< Never reaches here
request.object.set("primary", true);
} else {
console.log("Hello"); // <<< Never reaches here
}
},
error: function(error) {
console.log("Bye");
response.error("Query failed. Error = " + error.message);
}
});
response.success();
});
The issue is that queries are async. You're not waiting for the query to finish before calling response.success().
Think of your call to firstPhoneNumberQuery.find() like putting eggs on to boil, the success and error blocks are what you'll do when it is done.
The call to response.success() is like telling the cleaners you are done and they can toss whatever is still "cooking".
Use promises to avoid this issue, as well as moving the response.success() call inside your query's success handler:
duplicationQuery.count({
// ... success / error as above
}).then(function() {
var firstPhoneNumberQuery = new Parse.Query(PhoneNumbersObject);
firstPhoneNumberQuery.equalTo('user', Parse.User.current());
// return the promise so we can keep chaining
return firstPhoneNumberQuery.find();
}).then(function(results) {
// ... success for find
// then call success() here
response.success();
}, function(error) {
// ... error block for find
});
As you can see, you can combine as many then() blocks as you need.
Learn more about promises and chaining here:
https://parse.com/docs/js_guide#promises

Cloud code with many deletes in loop, but response.success finishes first on parse.com

I have a query, and they query may return many items.
I can go through all of them and destroy them.
The problem is since destroy is Async, the response.success(); part is executed before all the destroys are executed, so not all items are really deleted.
How can I make it wait until the loop is done and then only response.success();
Thanks.
garageQuery2.find({
success: function(results) {
alert("Successfully retrieved " + results.length + " garages to delete.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
object.destroy({
success: function(myObject) {
// The object was deleted from the Parse Cloud.
},
error: function(myObject, error) {
// The delete failed.
// error is a Parse.Error with an error code and description.
}
});
}
response.success();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
Try to work with Promises
This code is based on this: https://www.parse.com/docs/js_guide#promises-series
garageQuery2.find().then(function(results) {
// Create a trivial resolved promise as a base case.
var promiseSeries = Parse.Promise.as();
// the "_" is given by declaring "var _ = require('underscore');" on the top of your module. You'll use Underscore JS library, natively supported by parse.com
_.each(results, function(objToKill) {
// For each item, extend the promise with a function to delete it.
promiseSeries = promiseSeries.then(function() {
// Return a promise that will be resolved when the delete is finished.
return objToKill.destroy();
});
});
return promiseSeries;
}).then(function() {
// All items have been deleted, return to the client
response.success();
});
Hope it helps

Resources