synchronous

node.js: readSync from stdin?

℡╲_俬逩灬. 提交于 2019-11-27 04:18:27
问题 Is it possible to synchronously read from stdin in node.js? Because I'm writing a brainfuck to JavaScript compiler in JavaScript (just for fun). Brainfuck supports a read operation which needs to be implemented synchronously. I tried this: const fs = require('fs'); var c = fs.readSync(0,1,null,'utf-8'); console.log('character: '+c+' ('+c.charCodeAt(0)+')'); But this only produces this output: fs:189 var r = binding.read(fd, buffer, offset, length, position); ^ Error: EAGAIN, Resource

How do I load JSON data synchronously with d3.js?

若如初见. 提交于 2019-11-27 02:49:31
问题 When my site first initializes, it queries a server to get back some data. I can't lay anything out on the page until this data gets back. With d3.js, I can use d3.json() to get my data, but because it's asynchronous, I need to put the entire page logic in the callback function. How do I request the data and wait for it to come back? 回答1: You're basically doing it the only way. The callback function has to be the one initiating the rest of your code. You don't need all your code in the

JQuery synchronous animation

感情迁移 提交于 2019-11-27 01:35:26
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. 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 animation finishes. Why do you need to do this? You should probably use jQuery's callback parameter and continue

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

ε祈祈猫儿з 提交于 2019-11-27 00:42:23
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 block), so the getAThing returns "undefined" and I want it to return the results of the query. I'm sure I could some

Waiting until a file is available for reading with Win32

你说的曾经没有我的故事 提交于 2019-11-26 22:05:25
问题 I'm watching a directory by calling ReadDirectoryChangesW synchronously. When a new file is available, I try to access it immediately with CreateFile with GENERIC_READ and FILE_SHARE_READ , but this gives me ERROR_SHARING_VIOLATION . The process that put the file in the watched directory does not finish writing by the time I try to read it. Is there any way to reliably wait until the file is available for reading? I can put the method into a loop like the one below, but I'm hoping there's a

Change the asynchronous jQuery Dialog to be synchronous?

十年热恋 提交于 2019-11-26 19:49:23
问题 Currently, I'm working to replace "alert'/"confirm" with the jquery dialog. But most of legacy codes is written in some asynchronous way, which make it difficult to change. Is there any way to make jquery dialog work in a synchronous way? ( don't use loop or callback function ) For example: function run() { var result = confirm("yes or no"); alert( result ); \\more codes here } In this example the alert and other codes will be executed after user's choice. If we use jquery dialog var result =

Asynchronous and Synchronous Terms

大城市里の小女人 提交于 2019-11-26 19:24:42
问题 I'm confused by the term asynchronous when related to programming. It seems to mean the opposite in programming terms as what it is defined as in the dictionary. For example, the word synchronous means: occurring at the same time; coinciding in time; contemporaneous; simultaneous. going on at the same rate and exactly together; recurring together. Yet, Wikipedia says: "In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are

getJSON Synchronous

我只是一个虾纸丫 提交于 2019-11-26 16:21:44
问题 GOAL: What I'm after is to get data from database and refresh main.php (more evident through draw_polygon) every time something is added in database (after $.ajax to submit_to_db.php). So basically I have a main.php that will ajax call another php to receive an array that will be saved to database, and a json call another php to return an array will be used by main.php. $(document).ready(function() { get_from_db(); $('#button_cancel').click(function(){ $.ajax({ url: 'submit_to_db.php', type:

Synchronous database queries with Node.js

為{幸葍}努か 提交于 2019-11-26 15:50:51
I have a Node.js/Express app that queries a MySQL db within the route and displays the result to the user. My problem is how do I run the queries and block until both queries are done before redirecting the user to the page they requested? In my example I have 2 queries that need to finish before I render the page. I can get the queries to run synchronously if i nest query 2 inside the 'result' callback of query 1. This however will become very convoluted when the number of queries increase. How do I go about running multiple (in this case 2) database queries synchronously without nesting the

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

不打扰是莪最后的温柔 提交于 2019-11-26 14:10:39
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 cannot be changed to return merely a promise or demand a callback parameter. How do you meet both