我有这种情况,我想知道一个承诺的状态.下面,该函数start仅someTest在它不再运行时调用(Promise未挂起).该start函数可以多次调用,但如果在测试仍在运行时调用它,它将不会等待并返回false
class RunTest {
start() {
retVal = false;
if (!this.promise) {
this.promise = this.someTest();
retVal = true;
}
if ( /* if promise is resolved/rejected or not pending */ ) {
this.promise = this.someTest();
retVal = true;
}
return retVal;
}
someTest() {
return new Promise((resolve, reject) => {
// some tests go inhere
});
}
}
Run Code Online (Sandbox Code Playgroud)
我找不到简单检查承诺状态的方法.喜欢的东西this.promise.isPending会很好:)任何帮助将不胜感激!
ECMAScript 2015规范提到关键字(或单词?)new.target正好3次 - 在14.2.3中 1次:
通常,Contains不会查看大多数函数表单但是,Contains用于检测ArrowFunction中的new.target,this和super用法.
在14.2.16中两次:
ArrowFunction不为arguments,super,this或new.target定义本地绑定.对ArrowFunction中的参数,super,this或new.target的任何引用 都必须解析为词汇封闭环境中的绑定
MDN提到它,但非常模糊,页面不完整.
巴别塔似乎不支持它.尝试在函数(箭头或其他)中使用new.target时出现语法错误.
它是什么,它应该如何使用?
有什么区别:
public void Method1<T>(class1 c, T obj) where T:Imyinterface
Run Code Online (Sandbox Code Playgroud)
和
public void Method2(class1 c, Imyinterface obj)
Run Code Online (Sandbox Code Playgroud)
?
使用第一种方法有什么好处?
假设我使用可观察的数据服务构建我的Angular应用程序,由服务器端点支持:
@Injectable()
export class TodoService {
todos: Observable<Todo[]>
private _todos: BehaviorSubject<Todo[]>;
constructor(private http: Http) {
this._todos = <BehaviorSubject<Todo[]>>new BehaviorSubject([]);
this.todos = this._todos.asObservable();
}
loadData() {
this.http.get(dataUrl).subscribe(data => this._todos.next(data));
}
}
Run Code Online (Sandbox Code Playgroud)
现在假设我的数据引用了一些其他模型,并通过其他一些服务(某种形式的多对多关系)公开:
interface ToDo {
title: string;
status: StatusEnum;
requiredResouces: Resource[];
}
interface Resource {
name: string;
todos: ToDo[];
}
@Injectable()
export class ResourcesService {
resources: Observable<Resource[]>
private _resources: BehaviorSubject<Resource[]>;
...
}
Run Code Online (Sandbox Code Playgroud)
现在,假设我将一个方法添加到"链接"待办事项和资源的服务,该服务将能够将更新状态推送到主题,但其他服务将不知道该更改.例如:
export class TodoService {
...
addResourceRequirement(todo: ToDo, resource: Resource) {
this.http.post(`${url}/${todo.id}/`, {addResource: resource.id})
.subscribe(() => this.loadData());
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个ember-cli 0.2.7使用Ember.js 1.12.0应用程序与一段代码看起来像:
controllers/cart.js
import Ember from 'ember';
export default Ember.Controller.extend({
footwearInCart: Ember.computed('model.@each.category', function() {
return this.get('model').any(product => product.get('category').includes('Footwear'));
})
});
Run Code Online (Sandbox Code Playgroud)
它遍历模型中的所有对象,如果其类别属性中包含"鞋类",则返回true.
我试图像这样测试它:
tests/unit/controllers/cart-test.js
import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';
var products = [Ember.Object.create({name: 'shoe', category: 'Footwear', subTotal: 10}), Ember.Object.create({name: 'shirt', subTotal: 20})];
var model = Ember.ArrayProxy.create({
content: Ember.A(products)
});
moduleFor('controller:cart', {
beforeEach() {
this.controller = this.subject();
}
});
test('footwearInCart property works', function(assert) {
this.controller.set('model', model);
assert.equal(this.controller.get('footwearInCart'), true, 'The footwearInCart function …Run Code Online (Sandbox Code Playgroud) 这是谷歌最近的采访问题:
我们将f(X,Y)定义为X和Y的二进制表示中的不同对应位的数量.例如,f(2,7)= 2,因为2和7的二进制表示分别是010和111.第一和第三位不同,因此f(2,7)= 2.
你得到一个N正整数的数组,A1,A2,...,AN.求所有对(i,j)的f(Ai,Aj)之和,使得1≤i,j≤N
例如:
A = [1,3,5]
我们回来
f(1,1)+ f(1,3)+ f(1,5)+ f(3,1)+ f(3,3)+ f(3,5)+ f(5,1)+ f (5,3)+ f(5,5)=
0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8
我能想到这个解决方案是O(n ^ 2)
int numSetBits(unsigned int A) {
int count = 0;
while(A != 0) {
A = A & (A-1);
count++;
}
return count;
}
int count_diff_bits(int a, int b)
{
int x = a ^ b;
return numSetBits(x);
} …Run Code Online (Sandbox Code Playgroud) 我想编写一个低级日志记录功能,如下所示:
DO_DBG("some string", val1, val2)
Run Code Online (Sandbox Code Playgroud)
我想要它做的是,出于性能原因,将指针存储到字符串而不是字符串的副本.这假设字符串是只读文字.为了防止人们不得不调试调试器,如果编译器可以抱怨如果第一个参数DO_DBG位于代码与文本等的可写段中,那将是很好的.我想知道是否存在这样做的机制.(我正在使用gcc 4.9.1,ld 2.24).
MDN暗示使用.setPrototypeOf()将对代码的未来性能产生不良影响.
我还读了一些关于为什么改变对象的[[Prototype]]会降低性能的问题.但是没有一个答案真正解释了在后台发生的事情.所以我想知道这是否也适用于新的对象.
特别是我真的喜欢做这样的事情:
var MyPrototype = {
method1 : function(){...},
method2 : function(){...},
...
};
var newObject = Object.setPrototypeOf({
property : 1,
property2 : 'text'
}, MyPrototype);
Run Code Online (Sandbox Code Playgroud)
不幸的是你不能这样做,Object.create因为它不接受普通的对象文字.
我的使用是否setPrototypeOf也降低了执行JS引擎的性能?
我正在研究一些kata,但我无法通过所有测试用例.
所以情况是:
给定任何数组,例如此数组:int[] a = {2, 3, 10, 2, 4, 8, 1},找到数组中的最大差异对,同时确保较大的值处于较高的索引而不是较低的值.
在这个例子中:10是最大的元素,1是最小的元素,因为10它在索引处2,1在索引处6,因此它不计数,因为较大的对在较低的索引处.因此,正确的答案是a[0],和a[2],最大的不同是10-2.
其他要求是数组大小N介于1和之间1_000_000,任何给定a[i]的介于-1_000_000和之间1_000_000
我写了这样的代码:
static int maxDifference(int[] a) {
//test array size
if (a.length < 1 || a.length > 1_000_000) return -1;
int[] oldArr = Arrays.copyOf(a, a.length);
Arrays.sort(a);
int max = a[a.length - 1];
if (max > 1_000_000 …Run Code Online (Sandbox Code Playgroud) ecmascript-6 ×5
javascript ×5
algorithm ×1
angular ×1
arrays ×1
bitwise-xor ×1
c ×1
c# ×1
ember-cli ×1
ember.js ×1
es6-promise ×1
gcc ×1
generics ×1
java ×1
macros ×1
observable ×1
phantomjs ×1
promise ×1
rxjs ×1