JavaScript async and sync--a simple code

Source: Internet
Author: User

JavaScript is an explanatory language.

Python is also an explanatory language.

For an explanatory language, you need to know the language's integrated development environment, such as the command line.

The sync in Python,

For x in [1,2,3,4,5,6,7,8,9,0]

y = 0

While y < 1000000:

Y + = 1

Print X

Async in javascript:

FS = require (' FS ');

Fs.writefile ('./write.txt ', ' Hello world! ', function (err) {

if (err) throw err;

Console.log (' Oh, it writes successfully! ');

Console.log ("I ' m async and I'll show later");

}

Console.log ("haha,in fact, I ' M sync and I'll show earlier");

Using Python and node respectively to run the above two pieces of code, you can feel the difference.

Personally think, callback callback, back to call again.

There is a saying, "You are waiting for me, I will come back", personally think is almost the async.

* * * anonymous function used as closure * * *

var Baz;

(function () {var foo = 10;  var bar = 2;  Baz = function () {return foo*bar; }; })();

Javascript has ***function level*** scope. * This means a variable defined within a function is ***not*** accessible ***outside*** of it.

try {

Console.log (foo);

}

catch (Err) {Console.log (err);} try {

Console.log (bar); } catch (Err) {Console.log (err);}

Javascript has ***lexcially level*** scope. * This means functions run in the scope they is ***defined in***, ***not*** the scope they is ***excuted in***. Console.log (Baz ());

JavaScript Async and sync--a simple code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.