来自 uuid 的context.awsRequestId
真的是独一无二的吗?我想将它与资源的创建结合使用,所以我现在可以在创建资源时:
const uuid = require('uuid');
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.create = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
if (typeof data.text !== 'string') {
console.error('Validation Failed');
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: context.awsRequestId,
text: data.text,
checked: false,
createdAt: timestamp,
updatedAt: timestamp,
},
}; …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个扩展 RegExp 对象的 es6 模块。我正在使用Object.defineProperties
来做到这一点:
Object.defineProperties(RegExp.prototoype, {
...
});
Run Code Online (Sandbox Code Playgroud)
我不会返回此文件中的任何内容,因为我不需要它,因为 RegExp 在 中是全局的Node.js
,但同样,我需要导入一些内容来运行扩展 RegExp.prototype 的代码。你看到问题了吗?
在这种情况下,解决办法是什么?只需返回一个export default {}
? 这不是一件坏事吗?我正在考虑返回正则表达式并在导入中覆盖:
import RegExp from '@scope/regexp';
Run Code Online (Sandbox Code Playgroud)
谢谢。
我已经使用Promise很长一段时间了,它们总是我用来控制程序工作流程的"东西".例:
Promise
.resolve(obj)
.then(doSomething1)
.then(doSomething2)
.catch(handleError)
Run Code Online (Sandbox Code Playgroud)
而现在,我想改为试用风格,但我不知道究竟什么是正确的方法.
V1:
try {
var body = await Promise
.resolve(obj)
.then(doSomething1)
.then(doSomething2)
} catch (error) {
callback(error)
}
callback(null, {
statusCode: 200,
body
})
Run Code Online (Sandbox Code Playgroud)
V2:
try {
var body = await Promise
.resolve(obj)
.then(doSomething1)
.then(doSomething2)
.then(body => {
callback(null, {
statusCode: 200,
body
})
})
} catch (error) {
callback(error)
}
Run Code Online (Sandbox Code Playgroud)
什么是正确的方法?
是否可以从forEach的返回中构造一个对象?我的状况:
const data = {}
object.rules.forEach(rule => {
data[rule.name] = { body: [], type: rule.type }
})
Run Code Online (Sandbox Code Playgroud)
我想怎么做:
const data = object.rules.forEach(rule => {
data[rule.name] = { body: [], type: rule.type }
})
Run Code Online (Sandbox Code Playgroud) 有没有什么方法可以将 .vimrc 的内容组织在多个文件中?
目前我的 .vimrc 包含所有内容:插件语句、重新映射、集合、函数、mapleader、自定义插件配置、自动组等。
我想组织多个文件,也许按语言。
谢谢你!
我正在创建一个Node.js模块来与我的API进行交互,我使用superagent模块来执行请求.这个怎么运作:
module.exports = data => {
return getUploadUrl()
.then(uploadFiles)
.then(initializeSwam)
function getUploadUrl() {
const request = superagent.get(....)
return request
}
function uploadFiles(responseFromGetUploadUrl) {
const request = superagent.post(responseFromGetUploadUrl.body.url)
// attach files that are in data.files
return request
}
function initializeSwam(responseFromUploadFilesRequest) {
// Same thing here. I need access data and repsonseFromUploadFilesRequest.body
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得我做错了,但我想不出更好的方法来达到同样的效果.
在while循环中使用await是不好的做法吗?
我有以下代码:
// this is inside a async function
try {
let res = await api.resource.create(...args) // create resource
do {
let res = await api.resource.show(res.body.id)
if (res.body.status === 'COMPLETED')
return res.body
} while(1)
} catch (err) {
errorHandler(err)
}
Run Code Online (Sandbox Code Playgroud)
我在这里有几个问题:
先感谢您.
node.js ×6
javascript ×5
async-await ×2
asynchronous ×2
promise ×2
lambda ×1
loops ×1
plugins ×1
vim ×1