本文整理匯總了TypeScript中es6-sleep.promise函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript promise函數的具體用法?TypeScript promise怎麽用?TypeScript promise使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了promise函數的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: sleep
test.serial('cron should trigger the `onIdle` handler only once', async (t) => {
let count = 0;
const c = new MongoCron({
collection: t.context.collection,
lockDuration: 0,
onIdle: () => count++,
});
await c.start();
await sleep(1000);
await c.stop();
t.is(count, 1);
});
示例2: sleep
(async function() {
const mongo = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true });
const db = mongo.db('test');
const collection = db.collection('jobs');
const cron = new MongoCron({
collection,
onDocument: (doc) => console.log('onDocument', doc),
onError: (err) => console.log(err),
onStart: () => console.log('started ...'),
onStop: () => console.log('stopped'),
nextDelay: 1000,
reprocessDelay: 1000,
idleDelay: 10000,
lockDuration: 600000,
});
await collection.insertMany([
{ name: 'Job #3',
sleepUntil: moment().add(3, 'seconds').toDate()
},
{ name: 'Job #1',
sleepUntil: null
},
{ name: 'Job #2',
sleepUntil: moment().add(2, 'seconds').toDate()
},
{ name: 'Job #4',
sleepUntil: moment().add(8, 'seconds').toDate()
},
]);
cron.start();
await sleep(30000);
cron.stop();
})().catch(console.error);