asap
module that allows you to assign actions to be performed “as soon as right away” in an asynchronous manner. In Node.js, you can do this with the help of process.nextTick
, there are also different approaches that work in many browsers. We will create a module that will work in any environment. one export default
construct. var asap; var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]"; if (isNode) { asap = process.nextTick; } else if (typeof setImmediate !== "undefined") { asap = setImmediate; } else { asap = setTimeout; } export default asap;
asap
from another module, we use the following syntax: import asap from "asap"; asap(function() { console.log("hello async world!"); });
asap
module and stores it in the asap
variable, which we can later call.jQuery
function) and several additional named exports ( ajax
, getJSON
, animate
, etc.). In the Node.js module mkdirp
there is a default export that creates a directory and a named export called sync
, which does the same, but in sync.asap
module can also provide a function later
, which assigns the execution of the code at the time when other network or UI processes have already occurred. var asap; var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]"; if (isNode) { asap = process.nextTick; } else if (typeof setImmediate !== "undefined") { asap = setImmediate; } else { asap = setTimeout; } export default asap; export var later = isNode ? process.setImmediate : asap;
later
, we can import it in another module. import { later } from "asap"; later(function() { console.log("Running after other network events"); });
import asap, { later } from "asap";
import { unlink as rm } from "fs"; rm(filename, function(err) { /* check errors */ });
import * as fs from "fs"; fs.unlink(filename, function(err) { /* check errors */ });
var
or function
) a named export preceding it with the export
keyword. // exports this function as "requestAnimationFrame" export function requestAnimationFrame() { // cross-browser requestAnimationFrame } // exports document.location as "location" export var location = document.location;
class
or let
// exports this class as "File" export class File() { /* implementation */ } // exports "0.6.3" as "VERSION" export let VERSION = "0.6.3";
function getJSON() { // implementation } function postJSON() { // implementation } function animate() { // implementation }
module fs from "fs";
Source: https://habr.com/ru/post/229329/
All Articles