本文整理汇总了TypeScript中co.wrap函数的典型用法代码示例。如果您正苦于以下问题:TypeScript wrap函数的具体用法?TypeScript wrap怎么用?TypeScript wrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wrap函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
UserSchema.pre('save', function(done) {//done ~ next
let user = this;//isModified is mongoose function to check wether the document have been edited or not
//this refers to the document currently in process for updation, isModified @argument : <path>
if (!user.isModified('password')) {
return done();
}
/*using co wrap to use the generator function asynchronously as a promise intead
of using nested callbacks of bcrypt for salting and hashing*/
co.wrap(
function* () {
try {
var saltUser = yield bcrypt.genSalt(); //args include rounds currently defaulted to 10
var hashPassword = yield bcrypt.hash(this.password, saltUser); //hashing the function
this.password = hashPassword;
this.email = this.email.toLowerCase();
Promise.resolve(true);
}
catch (error) {
Promise.reject(error);
}
}
).call(this).then(done, function(error) {//call calls the co function which returns a promise
done(error);
});
});
示例2: function
const asyncJestTest = function(done: DoneFn) {
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
const returnValue = wrappedFn.call({});
if (isPromise(returnValue)) {
returnValue.then(done.bind(null, null), (error: Error) => {
const {isError: checkIsError, message} = isError(error);
if (message) {
extraError.message = message;
}
if (jasmine.Spec.isPendingSpecException(error)) {
env.pending(message!);
done();
} else {
done.fail(checkIsError ? error : extraError);
}
});
} else if (returnValue === undefined) {
done();
} else {
done.fail(
new Error(
'Jest: `it` and `test` must return either a Promise or undefined.',
),
);
}
};
示例3: buildImage
wrap<BuildConfig>(function* (
config: BuildConfig,
opts: BuildOptions,
name: string,
buildDir: string,
configDir: string
): IterableIterator<Promise<any>> {
const imageName = config.prefix ? `${config.prefix}${name}` : name;
console.log(`\n\n--> building ${imageName}\n`);
const imageConfig = getImageConfig(config, name);
const {version, dockerfile, isTemplate} = imageConfig;
const newVersion = version + 1;
const imageTag = `${imageName}:v${newVersion}`;
let dockerfilePath: string;
if (isTemplate) {
dockerfilePath = yield renderDockerfile(imageConfig, config, buildDir, configDir);
} else {
dockerfilePath = resolve(configDir, dockerfile);
}
yield buildImage(dockerfilePath, imageTag, config, opts);
let updatedConfig = assocPath(['images', name, 'version'], newVersion, config);
const childImageNames = findChildImages(name, updatedConfig.images);
for (const childImageName of childImageNames) {
updatedConfig = yield build(updatedConfig, opts, childImageName, buildDir, configDir);
}
return updatedConfig;
});
示例4: Promise
return new Promise((resolve, reject) => {
timeoutID = setTimeout(
() => reject(_makeTimeoutMessage(timeout, !!isHook)),
timeout,
);
// If this fn accepts `done` callback we return a promise that fulfills as
// soon as `done` called.
if (fn.length) {
const done = (reason?: Error | string): void => {
const errorAsErrorObject = checkIsError(reason)
? reason
: new Error(`Failed: ${prettyFormat(reason, {maxDepth: 3})}`);
// Consider always throwing, regardless if `reason` is set or not
if (completed && reason) {
errorAsErrorObject.message =
'Caught error after test environment was torn down\n\n' +
errorAsErrorObject.message;
throw errorAsErrorObject;
}
return reason ? reject(errorAsErrorObject) : resolve();
};
return fn.call(testContext, done);
}
let returnedValue;
if (isGeneratorFn(fn)) {
returnedValue = co.wrap(fn).call({});
} else {
try {
returnedValue = fn.call(testContext);
} catch (error) {
return reject(error);
}
}
// If it's a Promise, return it. Test for an object with a `then` function
// to support custom Promise implementations.
if (
typeof returnedValue === 'object' &&
returnedValue !== null &&
typeof returnedValue.then === 'function'
) {
return returnedValue.then(resolve, reject);
}
if (!isHook && returnedValue !== void 0) {
return reject(
new Error(
`
test functions can only return Promise or undefined.
Returned value: ${String(returnedValue)}
`,
),
);
}
// Otherwise this test is synchronous, and if it didn't throw it means
// it passed.
return resolve();
})
示例5: getImageConfig
import {exec} from './ShellUtil';
export const renderDockerfile: (
imageConfig: ImageConfig,
buildConfig: BuildConfig,
dpath: string,
cpath: string
) => Promise<string> =
wrap<string>(function* (
imageConfig: ImageConfig,
buildConfig: BuildConfig,
dpath: string,
cpath: string) {
const {version, dockerfile, baseimage} = imageConfig;
const baseimageConf = getImageConfig(buildConfig, baseimage);
const renderedDockerfilePath = join(dpath, `${basename(dockerfile)}-${Date.now()}`);
const locals = {
baseimage_version: baseimageConf.version
};
yield renderTmplToFile(resolve(cpath, dockerfile), locals, renderedDockerfilePath);
return renderedDockerfilePath;
});
export const buildImage: (
dfpath: string,
tag: string,
config: BuildConfig,
opts: BuildOptions
) => Promise < void> =
wrap<void>(function* (