本文整理匯總了TypeScript中co.default方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript co.default方法的具體用法?TypeScript co.default怎麽用?TypeScript co.default使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類co
的用法示例。
在下文中一共展示了co.default方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: createDefaultDbDocs
export function createDefaultDbDocs(db) {
co(function *() {
let users = yield db.collection('users').find({}).toArray();
// If the users array is empty
if (users.length === 0) {
console.log('No users in the database. Creating default users now...');
let users = [
{username: 'filip.lauc93@gmail.com', password: 'filip', status: 'offline', profileImage: 0},
{username: 'wojtek.kwiatek@gmail.com', password: 'wojtek', status: 'offline', profileImage: 0},
{username: 'laco0416@gmail.com', password: 'suguru', status: 'offline', profileImage: 0},
{username: 'mgualtieri7@gmail.com', password: 'mary', status: 'offline', profileImage: 0},
{username: 'ran.wahle@gmail.com', password: 'ran', status: 'offline', profileImage: 0}
];
for (let i = 0; i < users.length; i++) {
users[i].profileImage = generateRandomInt(1, 49);
users[i].password = yield hashPass(users[i].password);
}
let r = yield db.collection('users').insertMany(users);
if (r.insertedCount === users.length) console.log('...users created successfully.')
}
}).catch((err) => console.log(err))
}
示例2: Promise
//.........這裏部分代碼省略.........
//
// console.log(`${flagsPrompt}\n`);
//
// let appFlags = (yield prompt("App Flags: ")) || "";
//
// while (appFlags.search(allowedFlagsValidator) === -1) {
// console.log(chalk.red(`\nInvalid flags provided. Provide each flag only once,\nand only provide the allowed flags for the ${appTypeName} app type.\n`));
// appFlags = (yield prompt("App Flags: ")) || "";
// }
toReturn = {
json: {
appName: appName,
appType: appTypeName
},
generateApp: generateApp
// appFlags: appFlags
}
}
else {
for (let i = 0; i < prompts.length; i++) {
values[prompts[i].name] = (yield prompt(prompts[i].question)) || prompts[i].value;
while (values[prompts[i].name].search(prompts[i].validator) === -1) {
if (prompts[i].message) console.log(chalk.red(introMessage(prompts[i].message)));
values[prompts[i].name] = (yield prompt(prompts[i].question)) || prompts[i].value;
}
}
}
toReturn.json.appFolder = values.appFolder;
toReturn.json.bootLocation = values.bootFile;
toReturn.json.defaultFolders = {
components: values.componentsFolder,
services: values.servicesFolder,
directives: values.directivesFolder,
pipes: values.pipesFolder
};
return toReturn;
}).then(values => {
if (values.generateApp) {
co(function *() {
let appArray = [];
// TODO Finish flag implementation
// Files per flag
// flagsForType = {
// "standard": {
// t: [createFile(flags[values.json.appType].t, "tslint", "json")],
// g: [createFile(flags[values.json.appType].g, "gulpfile", "js")]
// },
// "npm library": {
// t: [createFile(flags[values.json.appType].t, "tslint", "json")],
// g: [createFile(flags[values.json.appType].g, "gulpfile", "js")]
// }
// };
switch (values.json.appType) {
case "standard":
appArray = [
createFile(createTemplateStringFromObject(values.json), "ng2config", "json"),
createFile(index(values.json.appFolder, values.json.bootLocation.slice(0, -3), values.json.appName), "index", "html"),
示例3: Promise
return new Promise((resolve, reject) => {
let comp = location.split("/");
// If the length is 1 then only the one file needs to be created
if (comp.length === 1) {
fs.writeFile(`${currentLocation}/${location}.${type}`, file,
err => reject(err),
() => resolve("Created successfully")
);
}
else {
let path = `${currentLocation}/`,
fileName = `${comp[comp.length - 1]}.${type}`;
// Add all of the other params to the path except for the last one which is the name of the file
for (let i = 0; i < comp.length - 1; i++) path += `${comp[i]}/`;
co(function* (){
yield mkdirp(path);
})
.catch(err => console.log(err.stack))
.then(() => {
fs.writeFile(`${currentLocation}/${location}.${type}`, file,
(err) => reject(err),
() => resolve("Created successfully")
)
})
}
})
示例4: co
}).then(values => {
if (values.generateApp) {
co(function *() {
let appArray = [];
// TODO Finish flag implementation
// Files per flag
// flagsForType = {
// "standard": {
// t: [createFile(flags[values.json.appType].t, "tslint", "json")],
// g: [createFile(flags[values.json.appType].g, "gulpfile", "js")]
// },
// "npm library": {
// t: [createFile(flags[values.json.appType].t, "tslint", "json")],
// g: [createFile(flags[values.json.appType].g, "gulpfile", "js")]
// }
// };
switch (values.json.appType) {
case "standard":
appArray = [
createFile(createTemplateStringFromObject(values.json), "ng2config", "json"),
createFile(index(values.json.appFolder, values.json.bootLocation.slice(0, -3), values.json.appName), "index", "html"),
createFile(createTemplateStringFromObject(tsconfig), "tsconfig", "json"),
createFile(createTemplateStringFromObject(packageJson(values.json.appName)), "package", "json"),
createFile(createTemplateStringFromObject(typings), "typings", "json"),
createFile(boot, `${values.json.appFolder}/boot`, "ts"),
createFile(appComponent, `${values.json.appFolder}/app.component`, "ts")
];
break;
case "npm library":
break;
}
// If flags were provided create the reguired files
if (values.appFlags) values.appFlags.split("").forEach(a => flagsForType[values.appType][a].forEach(b => appArray.push(b)));
yield appArray
})
.catch(err => reject(err.stack))
.then(() => {
console.log(chalk.green(`\nApplication created. Attempting to run scripts now.`));
resolve(false)
});
}
else {
createFile(createTemplateStringFromObject(values.json), "ng2config", "json")
.catch(err => reject(err))
.then(() => {
console.log(chalk.green(`\nng2config.json created successfully.`));
resolve(true)
});
}
})
示例5: Promise
return new Promise((resolve, reject) => {
if (!doInject) return resolve(value);
co(function *() {
let data;
try {
data = yield promiseFsReadFile(location)
} catch (err) {
throw err;
}
data = data.toString().split('\n');
data.splice(lineNumber, 0, items[item].types[type].html);
let full = data.join('\n');
return yield promiseFsWriteFile(location, full)
})
.then(res => resolve(value))
.catch(err => reject(err))
})