const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
let asyncFn = async () => {
while(1) {
await snooze(1000);
console.log(Date.now());
}
}
let asyncPromise = asyncFn();
setTimeout(() => {
console.log("try destroy");
asyncPromise = null;
}, 1500);
'use strict';
const assert = require('assert');
const fs = require('fs');
test();
function test() {
const a = fs.createWriteStream('a.txt');
for (let i = 0; i < 5; ++i)
a.write('line\n', (e) => console.log('Write, e: ', e));
a.close();
a.on('open', () => a.close());
a.on('error', console.error);
}
return function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(req, res) {
var result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return action(req, res);
case 3:
result = _context.sent;
return _context.abrupt('return', result);
case 7:
_context.prev = 7;
_context.t0 = _context['catch'](0);
console.error('API error', _context.t0);
return _context.abrupt('return', res.status(500).json({
message: _context.t0.message || 'Server error',
code: _context.t0.code,
stack: _context.t0.stack
}));
case 11:
case 'end':
return _context.stop();
}
}
}, _callee, _this, [[0, 7]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();
let res1 = null, res2= null;
try {
res1 = await action1();
res2 = await action2(res1);
} catch (e) {
if(e instanceof AsyncCancellationError) {
if (res1 !== null) {
console.log('action1 executed');
// но action2 то все равно надо где-то вызвать!!!
// вариант 1:
queue.push(async () => { await action2(res1) })
// вариант 2:
await new Promise(resolve => setTimeout(resolve, 500));
res2 = await action2(res1); // callback-hell от которого как-раз и пытались избавиться
}
if (res2 !== null) console.log('action2 executed');
}
}
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
async function asyncThread1() {
await snooze(500);
console.log(Date.now(), "sleep 500");
}
async function asyncThread2() {
await snooze(600);
console.log(Date.now(), "sleep 600");
}
asyncThread1();
asyncThread2();
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
const snoozeCancelable = (ctx, ms) => new Promise(
(resolve, reject) => {
let timer = setTimeout(resolve, ms);
ctx.cancel = () => {
clearTimeout(timer);
reject("canceled");
}
}
);
let ctx1 = {};
async function asyncThread1(ctx) {
try {
await snoozeCancelable(ctx, 1000);
console.log(Date.now(), "sleep 500");
}
catch(e) {
console.log("exception", e);
}
}
async function asyncThread2() {
await snooze(500);
ctx1.cancel();
console.log(Date.now(), "sleep 500, canceling first async thread");
}
asyncThread1(ctx1);
asyncThread2();
Each message is processed completely before any other message is processed. This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be pre-empted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it can be stopped at any point to run some other code in another thread.