synchronous

Why are javascript promises asynchronous when calling only synchronous functions?

喜夏-厌秋 提交于 2019-11-26 14:01:51
问题 In testing I've found that JavaScript Promises are always asynchronous regardless of whether or not they contain any asynchronous functions in their chain. Here is some code that shows the order of operations in console. If you run it you will see that even though every function is synchronous the output shows both of the aPromise() calls being run in parallel, and "surprisingly this happens after run 2 finishes" not happening before run 2 finishes. function aPromise() { return new Promise

Why is node.js asynchronous?

徘徊边缘 提交于 2019-11-26 12:36:19
问题 Nobody has actually asked this (from all the \'suggestions\' I\'m getting and also from searching before I asked here). So why is node.js asynchronous? From what I have deduced after some research: Languages like PHP and Python are scripting languages (I could be wrong about the actual languages that are scripting languages) whilst JavaScript isn\'t. (I suppose this derives from the fact that JS doesn\'t compile?) Node.js runs on a single thread whilst scripting languages use multiple threads

JQuery synchronous animation

梦想与她 提交于 2019-11-26 12:27:54
问题 In many cases I wish animation to be executed synchronously. Especially when I wish to make a a series of sequential animations. Is there an easy way to make a jQuery animate function call synchronous? The only way I thought about is to set a flag true when the animation has finished and to wait for this flag. 回答1: jQuery cannot make synchronous animations. Remember that JavaScript runs on the browser's UI thread. If you make a synchronous animation, the browser will freeze until the

Some clarification needed about synchronous versus asynchronous asio operations

丶灬走出姿态 提交于 2019-11-26 12:02:29
问题 As far as I know, the main difference between synchronous and asynchronous operations. I.e. write() or read() vs async_write() and async_read() is that the former, don\'t return until the operation finish -or error-, and the last ones, returns immediately. Due the fact that the asynchronous operations are controlled by an io_service.run() that does not finish until the controlled operations has finalized. It seems to me that in sequential operations as those involved in TCP/IP connections

Simplest way to wait some asynchronous tasks complete, in Javascript?

ε祈祈猫儿з 提交于 2019-11-26 11:35:33
I want to drop some mongodb collections, but that's an asynchronous task. The code will be: var mongoose = require('mongoose'); mongoose.connect('mongo://localhost/xxx'); var conn = mongoose.connection; ['aaa','bbb','ccc'].forEach(function(name){ conn.collection(name).drop(function(err) { console.log('dropped'); }); }); console.log('all dropped'); The console displays: all dropped dropped dropped dropped What is the simplest way to make sure all dropped will be printed after all collections has been dropped? Any 3rd-party can be used to simplify the code. freakish I see you are using mongoose

How to make JQuery-AJAX request synchronous

十年热恋 提交于 2019-11-26 11:21:59
问题 How do i make an ajax request synchronous? I have a form which needs to be submitted. But it needs to be submitted only when the user enters the correct password. Here is the form code: <form name=\"form\" action=\"insert.php\" method=\"post\" onSubmit=\"return ajaxSubmit(this);\" > And the jquery code for sending and checking password is this: var ajaxSubmit = function(formE1) { var password = $.trim($(\'#employee_password\').val()); $.ajax({ type: \"POST\", async: \"false\", url: \

Can AFNetworking return data synchronously (inside a block)?

孤人 提交于 2019-11-26 11:14:19
I have a function using AFJSONRequestOperation, and I wish to return the result only after success. Could you point me in the right direction? I'm still a bit clueless with blocks and AFNetworking specifically. -(id)someFunction{ __block id data; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json){ data = json; return data; // won't work } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ }]; NSOperationQueue *queue = [[NSOperationQueue alloc] init

What is the right way to make a synchronous MongoDB query in Node.js?

十年热恋 提交于 2019-11-26 09:26:55
问题 I\'m using the Node.JS driver for MongoDB, and I\'d like to perform a synchronous query, like such: function getAThing() { var db = new mongo.Db(\"mydatabase\", server, {}); db.open(function(err, db) { db.authenticate(\"myuser\", \"mypassword\", function(err, success) { if (success) { db.collection(\"Things\", function(err, collection) { collection.findOne({ name : \"bob\"}, function(err, thing) { return thing; }); }); } }); }); } The problem is, db.open is an asychronous call (it doesn\'t

How to wrap async function calls into a sync function in Node.js or Javascript?

纵然是瞬间 提交于 2019-11-26 08:51:27
问题 Suppose you maintain a library that exposes a function getData . Your users call it to get actual data: var output = getData(); Under the hood data is saved in a file so you implemented getData using Node.js built-in fs.readFileSync . It\'s obvious both getData and fs.readFileSync are sync functions. One day you were told to switch the underlying data source to a repo such as MongoDB which can only be accessed asynchronously. You were also told to avoid pissing off your users, getData API

asynchronous vs non-blocking

泪湿孤枕 提交于 2019-11-26 02:59:20
问题 What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)? 回答1: In many circumstances they are different names for the same thing, but in some contexts they are quite different. So it depends. Terminology is not applied in a totally consistent way across the whole software industry. For example in the classic sockets API, a non-blocking socket is one that simply returns immediately with a special "would block" error