在您的网页上实施基础代码后,您可以使用SDK开始执行命令。 您无需等待外部文件(alloy.js
)从服务器加载。 如果SDK尚未完成加载,则SDK会尽快排入队列并处理命令。
使用以下语法执行命令。
alloy("commandName", options);
的 commandName
告知SDK该做什么,而 options
是要传递到命令的参数和数据。 由于可用选项取决于命令,请查阅文档以了解有关每个命令的更多详细信息。
承诺 是SDK如何与您网页上的代码进行通信的基础。 promise是一种常见的编程结构,并非特定于此SDK甚至JavaScript。 promise用作创建promise时未知值的代理。 一旦该值已知,promise将通过该值“解析”。 处理程序函数可以与promise关联,以便在promise已解析或在解析promise过程中出错时向您发送通知。 要了解有关承诺的更多信息,请阅读 本教程 或Web上的任何其他资源。
每次执行命令时,都会返回一个promise。 promise表示命令的最终完成。 在以下示例中,您可以使用 then
和 catch
确定命令何时成功或失败的方法。
alloy("commandName", options)
.then(function(result) {
// The command succeeded.
// "value" is whatever the command returned
})
.catch(function(error) {
// The command failed.
// "error" is an error object with additional information
});
如果知道命令何时成功对您不重要,则可以删除 then
呼叫。
alloy("commandName", options)
.catch(function(error) {
// The command failed.
// "error" is an error object with additional information
});
同样,如果知道命令何时失败对您来说并不重要,则可以删除 catch
呼叫。
alloy("commandName", options)
.then(function(result) {
// The command succeeded.
// "value" will be whatever the command returned
});
从命令返回的所有承诺都使用 result
对象。 结果对象将包含数据,具体取决于命令和用户的同意。 例如,库信息将作为以下命令中结果对象的属性传递。
alloy("getLibraryInfo")
.then(function(result) {
console.log(result.libraryInfo.version);
console.log(result.libraryInfo.commands);
console.log(result.libraryInfo.configs);
});
如果用户未针对特定目的给予同意,则承诺仍将得到解决;但是,响应对象将仅包含可在用户同意的上下文中提供的信息。