How to synchronize search / save parsing operations?

We write server code in nodejs and using Parse javascript sdk. Often we need to get or update various Parse objects. For example, select the user with the username "Example".

function fetchUser(username) { var User = Parse.Object.extend("User"); var query = new Parse.Query("User"); query.equalTo("username",username); query.first({ success: function(results) { console.log("Successfully retrieved " + results.length + " scores."); return results; }, error: function(error) { console.log("Error: " + error.code + " " + error.message); } }); } 

This function may be called by another function:

 function test() { var user = fetchUser("example"); console.log(user); // would often print undefined return user; } function top() { // some code var user = test(); //some code } // and the function top() mmight be called by some other functions 

The problem is that I get undefined results because the find / first operations are asynchronous. Is there a way to ensure that the fetchUser () function returns only when the search / first operation is successful / completed?

+5
source share
2 answers

NodeJs is single-threaded, so we should not block the process.

in your case, you have two options: 1. passing a callback 2. using the Promise library ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise )

Example 1: transferring a callback

  function fetchUser(username, callback) { var User = Parse.Object.extend("User"); var query = new Parse.Query("User"); query.equalTo("username",username); query.first({ success: function(results) { console.log("Successfully retrieved " + results.length + " scores."); callback(null,results) }, error: function(error) { console.log("Error: " + error.code + " " + error.message); callback(error) } }); } 

Client Code:

  function test() { var user = fetchUser("example", function(error, user){ if(error){ console.error(error) }else { console.log(user) } }); } 

Example 2: Using the Promising Library

  function fetchUser(username){ return new Promise(function(resolve, reject){ var User = Parse.Object.extend("User"); var query = new Parse.Query("User"); query.equalTo("username",username); query.first({ success: function(results) { console.log("Successfully retrieved " + results.length + " scores."); resolve(results) }, error: function(error) { console.log("Error: " + error.code + " " + error.message); reject(error) } }); }) } 

Client Code:

 function test() { fetchUser("example") .then(function(user){ console.log(user) }) .catch(function(error){ console.error(error) }) } 
+3
source

JavaScript is inherently asynchronous. You should pass a callback to the fetchUser() function and rewrite the client code to something like this:

 function fetchUser(username, callback) { // do request and call callback(null, user) on success // or callback(some_error_object) on failure. } function test(callback) { var onFetched = function(err, user) { console.log(user); callback(err, user); } var user = fetchUser("example", onFetched); } 

You can also use promises or generators (since EcmaScript 6) is suitable.

UPD: Although some APIs (database drivers, fs, etc.) allow you to use them synchronously, they find it incorrect to write code in JavaScript. Since JS runs your code in one thread, blocking this thread with synchronous calls blocks all other tasks. So, if you create a web server that processes several clients at the same time and decides to use synchronous calls for some API, only one client will be served at a time, while other clients will be on hold.

+4
source

Source: https://habr.com/ru/post/1259158/


All Articles