我试图http.request融入Promise:
new Promise(function(resolve, reject) {
var req = http.request({
host: '127.0.0.1',
port: 4000,
method: 'GET',
path: '/api/v1/service'
}, function(res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
// First reject
reject(new Error('statusCode=' + res.statusCode));
return;
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
return;
}
resolve(body);
});
});
req.on('error', function(err) {
// Second reject
reject(err);
});
req.write('test');
}).then(function(data) {
console.log(data); …Run Code Online (Sandbox Code Playgroud) 有一个非常流行的问题是"std :: pair vs struct with two fields".但我有一个关于重新分配first和second值到语义命名变量的问题.在常规情况下,我们有这样的事情:
const std::pair<const int, const int> result = processSomething();
std::cout << result.second << " of " << result.first << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是如果我们首先将它们分配给引用变量呢:
const std::pair<const int, const int> result = processSomething();
const int &numTotal = result.first;
const int &numSuccessful = result.second;
std::cout << numSuccessful << " of " << numTotal << std::endl;
Run Code Online (Sandbox Code Playgroud)
这使我们免于撰写关于first和的语义的评论second.这种方式有什么缺点?编译器会为numTotal和保留堆栈numSuccessful吗?如果在主循环应用中使用此模式,性能是否会下降?
从常规变量更改为引用变量(感谢您的评论)
我们在 WSGI 模式下运行相同的 django 项目来处理 HTTP 请求,并在 ASGI 模式下处理 WebSockets。对于 WSGI 模式,我们使用gunicorn3服务器:
gunicorn3 --pythonpath . -b 0.0.0.0:8000 chat_bot.wsgi:application
Run Code Online (Sandbox Code Playgroud)
对于 ASGI 模式,我们使用daphne服务器:
daphne --root-path . -b 0.0.0.0 -p 8001 chat_bot.asgi:application
Run Code Online (Sandbox Code Playgroud)
如何以编程方式检测当前运行GreenUnicorn+WSGI或Daphne+ASGI 的模式?
DateTimeZone类中有奇怪的常量
class DateTimeZone {
const UTC = 1024;
const ALL = 2047;
...
}
Run Code Online (Sandbox Code Playgroud)
我试图找到有关它们的任何信息.也尝试过使用它们:
$dtz = new DateTimeZone(DateTimeZone::UTC); // throws Exception with message
// DateTimeZone::__construct(): Unknown or bad timezone (1024)
Run Code Online (Sandbox Code Playgroud)
要么
$dt = new Datetime('2016-02-01 10:00:00', DateTimeZone::UTC); // throws Exception with message
// DateTime::__construct() expects parameter 2 to be DateTimeZone, integer given
Run Code Online (Sandbox Code Playgroud)
它们是什么以及如何使用它们?
我创建域并运行它.现在,当输入域时process.domain指向当前活动域.但后来我运行Promise并得到一些奇怪的东西.
'use strict';
var domainCreate = require('domain').create;
var domain = domainCreate();
domain.requestId = 1;
domain.run(function() {
console.log(process.domain === domain); // true
console.log(process.domain.requestId); // 1
Promise.resolve().then(function() {
console.log(process.domain === domain); // false!!!
console.log(process.domain.requestId); // throw new TypeError('Cannot read property 'requestId' of undefined')
}).catch(function(err) {
console.error(err.stack);
});
});
Run Code Online (Sandbox Code Playgroud)
为什么process.domain在Promise链中变得不确定?
例如,模型有很多属性,但在 select 中我只指定了几个属性。
$listings = Realty::select('city', 'mls', 'class_id')->get();
Run Code Online (Sandbox Code Playgroud)
如何使一个特质(prefebable)或类继承 Eloquent,如果我尝试访问不在 select 中的属性,它将抛出异常:
$propertyType = $listings[0]->property_type; // returns `null`, but I need
// RuntimeException or ErrorException
Run Code Online (Sandbox Code Playgroud) 我以为我非常认识JavaScript,直到我遇到其他开发人员的代码让我失望:
var data = [];
Run Code Online (Sandbox Code Playgroud)
正如你可以看到它的名字,它应该被用作一个关联数组(即Object),但它是一个Array.然后他将值赋给该数组的键:
data['somekey'] = 'somevalue';
Run Code Online (Sandbox Code Playgroud)
我认为这在JavaScript中是不可能的,我认为它会引发异常,但它有效.它为什么有效?那么为什么我们需要对象,如果我们可以使用数组呢?它被认为是一种不好的做法,我应该羞辱那个开发者吗?
如果在调试控制台中的Kudu中的Microsoft Azure中输入"yes",它会挂起无限循环:
D:\home> yes
y
y
y
y
...
Run Code Online (Sandbox Code Playgroud)
为什么?