Call getSmiles() and getSmarts() Nested

Q

How to call getSmiles() and getSmarts() methods in 2 promises one nested in the other?

✍: FYIcenter.com

A

One way to avoid the bug mentioned in the previous tutorial is to call ketcher.getSmiles() and ketcher.getSmarts() methods in 2 promises one nested in the other.

This can be done by calling ketcher.getSmiles() first to create the first promise, and calling ketcher.getSmarts() inside both fulfilled and rejected handlers as shown in diagram below:

Create Nested Promises
Create Nested Promises

The following HTML document shows you how to call ketcher.getSmiles() and ketcher.getSmarts() in nested promises.

<html>
<!-- export-as-SMILES-SMARTS-nested.html Copyright (c) FYIcenter.com. -->
<head>
<title>Export Chemical Structure in SMILES and SMARTS Formats</title>
</head>
<body>
<p>Create a chemical structure, then click export buttons below the editor:</p>
<iframe id=frmKetcher src="/ketcher/index.html"
  style="width: 680px; height: 400px;"></iframe>

<p><button onclick="exportBoth();">Export in SMILES and SMARTS in nested promises</button></p>
<p>SMILES:</p>
<pre id=smiles style="background-color: #ddd;"></pre>
<p>SMARTS:</p>
<pre id=smarts style="background-color: #ddd;"></pre>
<script>
function exportBoth() {
  frm = document.getElementById("frmKetcher");
  ketcher = frm.contentWindow.ketcher;
  exportSMILES();
}

function exportSMILES() {
  let promise = ketcher.getSmiles();
  promise.then(
    function(value) {
      document.getElementById("smiles").innerHTML = value;
      exportSMARTS();
    },
    function(error) {
      document.getElementById("smiles").innerHTML = error.toString();
      exportSMARTS();
    }
  );
}

function exportSMARTS() {
  let promise = ketcher.getSmarts();
  promise.then(
    function(value) {
      document.getElementById("smarts").innerHTML = value;
    },
    function(error) {
      document.getElementById("smarts").innerHTML = error.toString();
    }
  );
}
</script>
</body>
</html>

Open the above HTML document on your local Web server. Draw a simple molecule structure in the editor.

Click "Export in SMILES and SMARTS in nested promises" button, you will see that both promises work correctly with SMILES and SMARTS strings returned to the page.

Call getSmiles() and getSmarts() in Nested Promises
Call getSmiles() and getSmarts() in Nested Promises

 

Call getSmiles() and getSmarts() Chained

Call getSmiles() and getSmarts() Parallelly

Ketcher JavaScript API

⇑⇑ Ketcher - Chemical Structure Editor

2024-01-24, 431🔥, 0💬