Created
November 10, 2015 07:16
-
-
Save zbinlin/b4c8283ba288dd91759b to your computer and use it in GitHub Desktop.
asyn run task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const format = date => date.toTimeString().split(" ")[0]; | |
/** | |
* @param {string} [name=fn.displayName||fn.name||"UNKNOW"] task name | |
* @param {(Function|*)} fn task function | |
* @param {...} [...args] task function's arguments | |
* @returns {} | |
*/ | |
export default async function run(name, fn, ...args) { | |
if (typeof name === "function") { | |
args.unshift(fn); | |
fn = name; | |
name = fn.displayName || fn.name || "UNKNOW"; | |
} | |
let start = new Date(); | |
console.log(`[${format(start)}] Starting ${name}...`); | |
let promise = typeof fn === "function" ? fn(...args) : fn; | |
let ret = await promise; | |
let end = new Date(); | |
console.log(`[${format(end)}] Finished ${name} after ${end - start}ms.`); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment