• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

TypeScript co.wrap函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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);
  });
});
开发者ID:girishgupta211,项目名称:LAAS_Server,代码行数:25,代码来源:user.ts


示例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.',
          ),
        );
      }
    };
开发者ID:Volune,项目名称:jest,代码行数:29,代码来源:jasmineAsyncInstall.ts


示例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;
});
开发者ID:d6u,项目名称:docker-build-layers,代码行数:35,代码来源:build.ts


示例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();
  })
开发者ID:facebook,项目名称:jest,代码行数:65,代码来源:utils.ts


示例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* (
开发者ID:d6u,项目名称:docker-build-layers,代码行数:31,代码来源:Util.ts



注:本文中的co.wrap函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript co-body.json函数代码示例发布时间:2022-05-24
下一篇:
TypeScript co.default函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap