From 21775e9b62afcbdecc47d17ecfef76ce5271823b Mon Sep 17 00:00:00 2001 From: jiangkai43 Date: Fri, 24 Feb 2023 16:56:59 +0800 Subject: [PATCH] Modify the code and function descriptions Signed-off-by: jiangkai43 --- .../reference/apis/js-apis-convertxml.md | 36 +++++++++------ .../reference/apis/js-apis-util.md | 46 +++++++++++++------ 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/js-apis-convertxml.md b/zh-cn/application-dev/reference/apis/js-apis-convertxml.md index d805a588e1..fccd142de8 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-convertxml.md +++ b/zh-cn/application-dev/reference/apis/js-apis-convertxml.md @@ -47,21 +47,27 @@ convertToJSObject(xml: string, options?: ConvertOptions) : Object **示例:** ```js -let xml = - '' + - '' + - ' Happy' + - ' Work' + - ' Play' + - ''; -let conv = new convertxml.ConvertXML() -let options = {trim : false, declarationKey:"_declaration", - instructionKey : "_instruction", attributesKey : "_attributes", - textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype", - commentKey : "_comment", parentKey : "_parent", typeKey : "_type", - nameKey : "_name", elementsKey : "_elements"} -let result = JSON.stringify(conv.convertToJSObject(xml, options)); -console.log(result); +try { + let xml = + '' + + '' + + ' Happy' + + ' Work' + + ' Play' + + ''; + let conv = new convertxml.ConvertXML() + let options = { + trim: false, declarationKey: "_declaration", + instructionKey: "_instruction", attributesKey: "_attributes", + textKey: "_text", cdataKey: "_cdata", doctypeKey: "_doctype", + commentKey: "_comment", parentKey: "_parent", typeKey: "_type", + nameKey: "_name", elementsKey: "_elements" + } + let result = JSON.stringify(conv.convertToJSObject(xml, options)); + console.log(result); +} catch (e) { + console.log(e.toString()); +} // 输出(宽泛型) // {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note","_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title","_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"text","_text":"Play"}]}]}]} ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-util.md b/zh-cn/application-dev/reference/apis/js-apis-util.md index c50d6b23af..02c2185ccd 100755 --- a/zh-cn/application-dev/reference/apis/js-apis-util.md +++ b/zh-cn/application-dev/reference/apis/js-apis-util.md @@ -26,7 +26,7 @@ format(format: string, ...args: Object[]): string | 参数名 | 类型 | 必填 | 说明 | | ------- | -------- | ---- | -------------- | | format | string | 是 | 式样化字符串。 | -| ...args | Object[] | 否 | 替换式样化字符串通配符的数据。 | +| ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 | **返回值:** @@ -69,6 +69,20 @@ let result = util.errnoToString(errnum); console.log("result = " + result); ``` +**部分错误码及信息示例:** + +| 错误码 | 信息 | +| ------ | -------------------------------- | +| -1 | operation not permitted | +| -2 | no such file or directory | +| -3 | no such process | +| -4 | interrupted system call | +| -5 | i/o error | +| -11 | resource temporarily unavailable | +| -12 | not enough memory | +| -13 | permission denied | +| -100 | network is down | + ## util.callbackWrapper callbackWrapper(original: Function): (err: Object, value: Object )=>void @@ -126,17 +140,23 @@ promisify(original: (err: Object, value: Object) => void): Function **示例:** ```js - function aysnFun(str1, str2) { - if (typeof str1 === 'object' && typeof str2 === 'object') { - return str2 - } else { - return str1 - } - } - let newPromiseObj = util.promisify(aysnFun); - newPromiseObj({ err: "type error" }, {value:'HelloWorld'}).then(res => { - console.log(res); - }) +function fun(num, callback) { + if (typeof num === 'number') { + callback(null, num + 3); + } else { + callback("type err"); + } +} + +const addCall = util.promisify(fun); +(async () => { + try { + let res = await addCall(2); + console.log(res); + } catch (err) { + console.log(err); + } +})(); ``` ## util.randomUUID9+ @@ -243,7 +263,7 @@ printf(format: string, ...args: Object[]): string | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | format | string | 是 | 式样化字符串。 | -| ...args | Object[] | 否 | 替换式样化字符串通配符的数据。 | +| ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 | **返回值:** -- GitLab