首页 文章

如何使用Nativecript与本机ES6 Promises

提问于
浏览
117

我是Typescript的完全初学者,我想知道是否可以在Typescript中使用ES6承诺以及我必须做些什么才能让它们起作用 . 我正在运行节点0.11.14并在编译期间收到错误“无法找到名称'承诺'”

8 回答

  • -1

    当前的lib.d.ts中没有定义的promise,所以你需要一个额外的定义文件,这就是你得到编译错误的原因 .

    例如,你可以使用esel-promise包和DefinitelyTyped的定义文件:es6-promise definition

    然后你可以像这样使用它:

    var p = new Promise<string>((resolve, reject) => { 
        resolve('a string'); 
    });
    

    edit 在针对ES6(使用TypeScript编译器)时,您可以在没有定义的情况下使用它 - 请注意,您仍然需要Promise存在于运行时期(因此它在旧浏览器中不起作用:))将以下内容添加/编辑到您的 tsconfig.json

    "compilerOptions": {
        "target": "ES6"
    }
    

    edit 2 当TypeScript 2.0问世时,事情会有所改变(虽然上面仍然有效),但定义文件可以直接用npm安装,如下所示:

    npm install --save @types/es6-promise - source

    edit3 使用更多信息更新答案以使用类型 .

    创建一个只有 { } 作为内容的 package.json 文件(如果你还没有package.json . 调用 npm install --save @types/es6-promisetsc --init . 第一个npm install命令将改变你的 package.json 以包含es6-promise作为依赖.tsc - init将为您创建一个 tsconfig.json 文件 .

    您现在可以在打字稿文件 var x: Promise<any>; 中使用promise . 执行 tsc -p . 以编译您的项目 . 你应该没有错误 .

  • 47

    备选方案#1

    使用 targetlib 编译器选项直接编译为 es5 ,无需安装 es6-shim . (使用TypeScript 2.1.4 测试) . 在lib部分中,使用 es2016es2015.promise .

    // tsconfig.json
    {
        "compilerOptions": {
            "target": "es5",
            "lib": [
                "es2015.promise",
                "dom"
            ]
        },
        "include": [
            "src/**/*.ts"
        ],
        "exclude": [
            "node_modules"
        ]
    }
    

    备选方案#2

    Use NPM to install es6-shim 来自types organization .

    npm install @types/es6-shim --save-dev
    

    备选方案#3

    在TypeScript 2.0之前,使用typingsDefinitelyTyped全局安装 es6-shim .

    npm install typings --global --save-dev
    typings install dt~es6-shim --global --save-dev
    

    typings 选项使用 npm 全局安装 typings ,然后使用 typings 安装填充程序 . dt~ 前缀表示从DefinitelyTyped下载填充程序 . --global 选项意味着垫片的类型将在整个项目中可用 .

    另见

    https://github.com/Microsoft/TypeScript/issues/7788 - 找不到名字'Promise'&找不到名字'require'

  • 120

    从TypeScript 2.0开始,您可以通过在 tsconfig.json 中包含以下内容来包含本机承诺的输入内容

    "compilerOptions": {
        "lib": ["es5", "es2015.promise"]
    }
    

    这将包括TypeScript附带的promise声明,而不必将目标设置为ES6 .

  • 13

    如果您使用node.js 0.12或更高版本/ typescript 1.4或更高版本,只需添加编译器选项,如:

    tsc a.ts --target es6 --module commonjs
    

    更多信息:https://github.com/Microsoft/TypeScript/wiki/Compiler-Options

    如果您使用 tsconfig.json ,那么像这样:

    {
        "compilerOptions": {
            "module": "commonjs",
            "target": "es6"
        }
    }
    

    更多信息:https://github.com/Microsoft/TypeScript/wiki/tsconfig.json

  • 6

    这是最近这样做的方法,上面的答案已经过时了:

    typings install --global es6-promise

  • 12

    A.如果使用 "target": "es5" 和TypeScript版本低于2.0:

    typings install es6-promise --save --global --source dt
    

    B.如果使用 "target": "es5" 和TypeScript版本2.0或更高版本:

    "compilerOptions": {
        "lib": ["es5", "es2015.promise"]
    }
    

    C.如果使用 "target": "es6" ,则无需执行任何操作 .

  • 23

    在Visual Studio 2015 Node.js工具1.2中使用Nativecript的原生ES6 Promises

    由于ES6 Promises是原生的,因此无需安装npm .

    Node.js project - >属性 - > Typescript构建选项卡ECMAScript版本= ECMAScript6

    import http = require('http');
    import fs = require('fs');
    
    function findFolderAsync(directory : string): Promise<string> {
    
        let p = new Promise<string>(function (resolve, reject) {
    
            fs.stat(directory, function (err, stats) {
    
                //Check if error defined and the error code is "not exists"
                if (err && err.code === "ENOENT") {
                    reject("Directory does not exist");
                }
                else {
                    resolve("Directory exists");
                }
            });
    
        });
        return p;
    }
    
    findFolderAsync("myFolder").then(
    
        function (msg : string) {
            console.log("Promise resolved as " + msg); 
        },
        function (msg : string) {
            console.log("Promise rejected as " + msg); 
        }
    );
    
  • 5

    我不得不将 @types/core-js 降级到9.36以使其与我的tsconfig中设置的 "target": "es5" 一起使用 .

    "@types/core-js": "0.9.36",

相关问题