条件:我正在使用Win10,TypeScript 1.8和Visual Studio Code 1.0.0。我有类似的代码
///<reference path = "./typings/lib.es6.d.ts" />
Run Code Online (Sandbox Code Playgroud)
然后
let z = "0".repeat(4 - str.length)
Run Code Online (Sandbox Code Playgroud)
这是let z线。
VS Code在“重复”下加上红色下划线并报告
[ts] Property 'repeat' does not exist on type 'string'.
Run Code Online (Sandbox Code Playgroud)
我在命令行上用
tsc <filename>.ts
Run Code Online (Sandbox Code Playgroud)
指向repeat该let z行的开头,编译器报告
error TS2339: Property 'repeat' does not exist on type 'string'.
Run Code Online (Sandbox Code Playgroud)
开始时有所不同,TypeScript,但这是ES2015(ES6)所添加的。
问题:如何获得干净的编译文件?
编辑:缩短。
总结:poll()具有回调函数可用; 我没有找到任何使用本机承诺.我试图适应一些没有成功.我还没有解决的问题是,当setTimeout调用的函数的第一个实例结束而没有任何返回时,.then()监听它会将终止视为a false和a reject().then()终止并且不听取以后的回报.
问题:如何最好地帮助.then()功能留下来以后用resolve()或返回reject()?
这篇文章的其余部分是详细的.阅读有用的内容.
可用的轮询功能:我喜欢(/sf/users/87445361/)Om Shankar 每60秒调用一次函数的响应.David Walsh的民意调查()非常相似(在https://davidwalsh.name/essential-javascript-functions).两者都使用回调并且运行良好.我在javascript
中发现
了一个包含poll()使用bluebird-only promises的民意调查.
这是我尝试用本机承诺实现的.
/**
* poll - checks repeatedly whether a condition exists. When the condition
* exists, returns a resolved standard promise. When it has checked
* long enough, returns a rejected standard promise.
* @param {function} fn - a caller-supplied synchronous …Run Code Online (Sandbox Code Playgroud) 这可能是一个有很多答案的问题,或者你需要看到我的实际代码来帮助我.对于我所看到的行为,可能有一个原因(或少数原因).第一个问题:这是什么?如果是前者,我会撤回这个问题,这样人们就不会浪费时间.无论如何,我无法分享代码并且它比适当的更长.
我正在使用bluebird for Promises在Node.JS中编写JavaScript.我的部分代码适合这个模型.
const Promise = require('bluebird');
function a() {
return new Promise(function(resolve, reject) {
<... do something>
<either> return resolve();
<or> return reject();
})
}
a()
.catch(error => { console.log('Used .catch error=', error) })
.then(result => { console.log('Used .then result=', result) });
Run Code Online (Sandbox Code Playgroud)
在我的代码的某些部分拟合这个模型,我看到一个日志语句的结果.在我的代码的其他部分适合这个模型,我也看不到.在后面的部分,当我跟踪执行路径与调试,它完成之后a,它把绿色的亮点就(error在.catch(error => {与next上}合包含无极a,它.then和它的.catch,并next在(后通过一些蓝鸟代码会)}闭幕包含相同Promise的函数.
JSHint没有识别任何相关内容.
当我使用本机Promise时,我看到了非常相似的行为.我解决了它,然后代替bluebird Promises.现在我看到蓝鸟在不同的地方做同样的事情.
如果这是一个已知且易于描述的原因,我将非常感谢您的帮助.如果它大于那个,这个问题可能不属于Stack Overflow; 我会撤回它.
提前致谢.
这是更多代码的子集; Node.js中的JavaScript 实质上,等效于函数a调用某些单元测试(在函数中b).返回时b,a调用异常测试(在函数中c).c调用同步异常测试(在函数中d).稍后,c将调用另一个函数(e比方说)进行异步异常测试(Promise reject()用法).似乎最好在Node.js中使用Promise,但即使使用它们也不总是导致我预测的行为.
'use strict';
function d() {
return new Promise(function(resolve, reject) {
console.log('start d throw test');
try {
throw new Error('Error type');
} catch (e) {
console.log('d catch block e.message=' + e.message +
' rejecting to c');
return reject(new Error('d ' + e.message));
} // catch
}) // Promise
}
function c() {
return new Promise(function(resolve, reject) {
console.log('start c'); …Run Code Online (Sandbox Code Playgroud) javascript ×3
promise ×3
node.js ×2
asynchronous ×1
bluebird ×1
callback ×1
exception ×1
typescript ×1